Quote:
Originally Posted by rgb
this is one way to do it (simplified):
int input=7;
int r=0, x=0;
while(input>0)
{
int y = i%2;
input /= 2;
x += y * pow(10,r++);
printf("%d\n",x);
}
where
"input" should be in some form of scanf( ) or other input command and statement "x += y * pow(10,r++);" should be change to some type of strcat( ) to prevent overflow.
This is a very poor solution
1. It does not compile (i should be input ?)
2. It goes wrong is input >= 0x400
3. Because it uses pow it decends into floating point arithmatic unnecessarily which could introduce rounding errors.
4. It uses an int to represent a decimal coded binary value (which is why 2 happens), in my opinion very poor style.
abbey, you sound like you have already made an attempt at doing this, ignore rgbs solution and post what you already have we will help you make it work.
You should be able to do it by using the bitwise operators to determin if any given binary digit should be 1 or 0 and then you can store the result in a string and then print it out when finished.