bit value generation doubt

• 08-07-2009
roaan
bit value generation doubt
I came across this link on how to generate the bitwise represenation of any int , char on one of the forums.

http://cboard.cprogramming.com/c-pro...-showbits.html

The logic that the program uses is

[insert]
Code:

``` void bits_uint(unsigned int value)  {     unsigned int bit;     for ( bit = /* msb */(~0U >> 1) + 1; bit > 0; bit >>= 1 )     {       putchar(value & bit ? '1' : '0');     }   putchar('\n'); }```
In the for loop the condition that the program uses is

for ( bit = /* msb */(~0U >> 1) + 1; bit > 0; bit >>= 1 )

What does this imply

bit >>= 1(greater than , greater than equal to ???????????????)

If i understand it now its supposed to mean

bit = bit >> 1;
• 08-07-2009
Salem
Well it's like += say.

And you are correct.
• 08-07-2009
MK27
bit = /* msb */(~0U >> 1) + 1;

Could also be written:

bit = /* msb */~(~0U>>1);

Which that makes slightly more obvious sense to me, since what you want is just the most significant bit set (eg, "1000000000"), which would be all bits set (~0), shift right once ("01111111111") and flip that (~).

By ANDing that with the value each time bit >>= 1, you can saying whether the corresponding bit in value is set.