To continue where BountyX left off, to convert from integer to binary, use what I call the "calculating change" method - because it reminds me of one of the first C++ programs I learned was given 200 cents, calculate the equivalent in the smallest number of coins.
i.e. is number larger than 128, if yes, then a 1 ... is what is left larger than 64, if no, then a 0 ... is what is left larger than 32 ... and, well, you get the idea ;)
cscgal
The Queen of DaniWeb
19,421 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 229
use the & and << operators. remember that a character is just a sequence of bits. u cant test for each bit and output the result.
int main()
{
char x = 'a';
int y;
// the value of 'a' in hex is 0x61, or 97 decimal
// in binary, that would be: 0 1 1 0 0 0 0 1
// or 64 + 32 + 1, or 2^7 + 2^6 + 2^0
//
//loop for number of bits in a character
//this will print out bits in reverse order
//least significant bit first
for(y = 0; y < sizeof(char) * 8; y++)
printf("%c ", ( x & (1 << y) ) ? '1' : '0' );
puts("");
return 0;
}
infamous
Junior Poster in Training
77 posts since Mar 2004
Reputation Points: 47
Solved Threads: 2