sure... ill try
Code:
void displayBits( unsigned value )
{
unsigned c, displayMask = 1 << 31; // set the bit mask so that we only compare the last bit
printf( "%7u = ", value );
// loop 32 time or the number of bits in a type unsigned
for ( c = 1; c <= 32; c++ ) {
// now we use the mask to test the last bit of "value" and see if its set(1) if it is putchar a 1 other wise a 0
putchar( value & displayMask ? '1' : '0' );
value <<= 1; // move the bits of "value one to the left i'll explain bit shifting below
// for ever eight bit(a byte) put a space to seperate.
if ( c % 8 == 0 )
putchar( ' ' );
}
putchar( '\n' );
}
bit shifting works like so
say i have the value
Code:
00000000 00000000 00000000 00000001 '1'
if is shift it one bit to the left (eg. "value <<= 1") i would get
00000000 00000000 00000000 00000010 '2'
lets say i shift in another ten bits "value <<= 10" i would get
00000000 00000000 00001000 00000000 '4096'
but what happens if i shift past the ends?
that bit is lost.
so if i shifted
10000000
left once (<< 1)
00000000
would be the result