[Tfug] Byte Order

Rich Smit rfs_lists at mac.com
Fri Nov 17 15:36:47 MST 2006


Thanks for making me look again at that Robert!

void print_bits(short int n, char better) {
   while (--better>=0) { printf( ((n & (1<<better)) ? "1" : "0") ); }
   printf("\n");
}

Yep, that's a bit better now...
-- 
Rich



Robert Hunter wrote:
>> but tried to keep the code somewhat readable
>> (yeah, except for that power-of-two business...)
> 
> Rich, here is a trick you can use in C. Multiplying or dividing by two is
> equivalent to bit-shifting.
> 
> e.g.
> 
> 2*2*2*2*2*2*2*2*2*2 == 1024 == 1 << 10
> 
> 1024/2/2/2/2/2/2/2/2/2/2 == 1 == 1024 >> 10
> 
> 
> 
> 





More information about the tfug mailing list