// PlayingWithBits.c - Show some bit manipulations
// Author:  Bary W Pollack
// Date:    Nov. 11, 2014

#include <stdio.h>


// binaryString places a string of 0's and 1's in str 
// equivalent to the binary representation of x
void binaryString(unsigned x, char str[])
{
	unsigned mask = 1;
	int adjust = 35;

	for (int i = 0; i < 32; i++)
	{
		// Insert a space every 8 bits
		if ((i % 8) == 0)
		{
			str[adjust - i] = ' ';
			--adjust;
		}
		str[adjust - i] = (x & mask) ? '1' : '0';
		mask = mask << 1;
	}
	str[35] = '\0';
}


// display a value as unsigned, hex, and binary
void display(unsigned x)
{
	char str[64];

	binaryString(x, str);
	printf("%12u    %08x    %s\n", x, x, str);
} // end display()


int main()
{
	unsigned x = 1; // The word we will play with

	printf("\nPlaying With Bits\n\n");
	printf("    unsigned  hexadecimal                 binary\n\n");
	while (x != 0)
	{
		display(x);
		x = x << 1;
	}
	printf("\nFini!\n\n");

	return 0;
} // end main()


/* ===== Runtime Output =====

Playing With Bits

  unsigned  hexadecimal                 binary

         1    00000001    00000000 00000000 00000000 00000001
         2    00000002    00000000 00000000 00000000 00000010
         4    00000004    00000000 00000000 00000000 00000100
         8    00000008    00000000 00000000 00000000 00001000
        16    00000010    00000000 00000000 00000000 00010000
        32    00000020    00000000 00000000 00000000 00100000
        64    00000040    00000000 00000000 00000000 01000000
       128    00000080    00000000 00000000 00000000 10000000
       256    00000100    00000000 00000000 00000001 00000000
       512    00000200    00000000 00000000 00000010 00000000
      1024    00000400    00000000 00000000 00000100 00000000
      2048    00000800    00000000 00000000 00001000 00000000
      4096    00001000    00000000 00000000 00010000 00000000
      8192    00002000    00000000 00000000 00100000 00000000
     16384    00004000    00000000 00000000 01000000 00000000
     32768    00008000    00000000 00000000 10000000 00000000
     65536    00010000    00000000 00000001 00000000 00000000
    131072    00020000    00000000 00000010 00000000 00000000
    262144    00040000    00000000 00000100 00000000 00000000
    524288    00080000    00000000 00001000 00000000 00000000
   1048576    00100000    00000000 00010000 00000000 00000000
   2097152    00200000    00000000 00100000 00000000 00000000
   4194304    00400000    00000000 01000000 00000000 00000000
   8388608    00800000    00000000 10000000 00000000 00000000
  16777216    01000000    00000001 00000000 00000000 00000000
  33554432    02000000    00000010 00000000 00000000 00000000
  67108864    04000000    00000100 00000000 00000000 00000000
 134217728    08000000    00001000 00000000 00000000 00000000
 268435456    10000000    00010000 00000000 00000000 00000000
 536870912    20000000    00100000 00000000 00000000 00000000
1073741824    40000000    01000000 00000000 00000000 00000000
2147483648    80000000    10000000 00000000 00000000 00000000

Fini!

===== End Runtime Output ===== */
