training

Code I wrote during training
git clone git://git.bitsmanent.org/training
Log | Files | Refs | README

shift.c (517B)


      1 /* Exercise 10.10 */
      2 
      3 #include <stdio.h>
      4 
      5 void tobits(int n);
      6 
      7 int main(void)
      8 {
      9    int num = 9;
     10 
     11    printf("tobits(%d):\t", num);
     12    tobits(num);
     13 
     14    num >>= 4;
     15    printf("\n");
     16 
     17    printf("tobits(%d):\t", num);
     18    tobits(num);
     19 
     20    printf("\n");
     21 
     22    return 0;
     23 } /* E0F main */
     24 
     25 /* Print a number in bits */
     26 void tobits(int n)
     27 {
     28    int i;
     29    unsigned mask = 1 << 31;
     30 
     31    for(i = 1; i <= 32; i++) {
     32       printf("%d", n & mask ? 1 : 0);
     33 
     34       if( !(i % 8) )
     35 	 putchar(' ');
     36 
     37       n <<= 1;
     38    }
     39 
     40 } /* eof tobits() */
     41