Printing binary char in c
I cannot, for the love of myself figure out what the logic is behind this
C code.
Supposedly, the following should print out the binary representation of an
unsigned char x, and we are only allowed to fill in the blanks.
void print_binary(unsigned char x) {
int b = 128;
while (__________________) {
if (b <= x) {
x -= b;
printf("1");
} else
printf("0");
______________________;
}
}
Of course I could game the program by simply ignoring the lines above.
However I'm under the impression that this is not really the way to do
things (it's more of a hack).
I mean really now, the first condition checks whether 128 is <= the char,
but isn't an unsigned char, what, 255 bytes? So why is it only printing
'1' in it.
Perhaps I'm missing something quite obvious (not really a c programmer)
but the logic just doesn't sink into me this time.
Can anyone point me in the right direction? And if you can give me a clue
without completely saying the answer, that would be heavenly.
No comments:
Post a Comment