/* Print the binary representation of a non-negative integer */

#include <stdio.h>
#include <limits.h>

#ifndef VALUE
#define VALUE 42
#endif

int
main(void)
{
   int n = VALUE;
   
   if (n<0) {
      printf("Error: n must be non-negative");
      return 1;
   }
   
   printf ("number of bits in int:  %ld\n", sizeof(int)*CHAR_BIT);

   printf ("bit pattern for n:  ");

   int int_size = CHAR_BIT*sizeof(int);

   // initialize mask with a 1 in the leftmost-but-one position
   unsigned int mask = 1 << (int_size - 2);
   printf ("0");                             // print a leading zero 
   for (int i = 0; i < int_size-1; i++) {    // for each bit:
       printf( (mask & n) ? "1" : "0" );     //     print the bit
       mask = mask >> 1;                     //     shift the mask rightward
    }
  printf ("\n");

  return 0;
} // main
