Re: print binary representation
Carramba wrote:
Think about it!
void bits(unsigned char b, int n) {
for (--n; n >= 0; --n)
putchar((b & 1 << n) ? '1' : '0');
putchar(' ');
}
Now if you call it..
bits(195, 8);
...you'll get '11000011 ' on the stdout stream.
--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Carramba wrote:
Harald van Dijk wrote:
thanx, maybe you have so suggestion or link for further reading on how
to do it?
>Carramba wrote:
>>
>There is no standard format specifier for binary form. You will have
>to do the conversion manually, testing each bit from highest to
>lowest, printing '0' if it's not set, and '1' if it is.
>>
>>Hi!
>>>
>>How can I output value of char or int in binary form with printf(); ?
>>>
>>thanx in advance
>>>
>>How can I output value of char or int in binary form with printf(); ?
>>>
>>thanx in advance
>There is no standard format specifier for binary form. You will have
>to do the conversion manually, testing each bit from highest to
>lowest, printing '0' if it's not set, and '1' if it is.
>>
to do it?
void bits(unsigned char b, int n) {
for (--n; n >= 0; --n)
putchar((b & 1 << n) ? '1' : '0');
putchar(' ');
}
Now if you call it..
bits(195, 8);
...you'll get '11000011 ' on the stdout stream.
--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Comment