
Some Additional Operations on Type int
--------------------------------------

Bitwise Logical Operators: &, |, ^, ~

Typically used when operating on binary values (as opposed to integer values)


The ~ operator performs a bitwise logical complement of its' single int operand.

   int indicators;

   indicators = ~0x4;      // 0x4 is:   0000 0000 0000 0100
                           // ~0x4 is:  1111 1111 1111 1011


The & operator performs a bitwise logical AND of its' two int operands. 

   int x, y, z;

   x = 0x66CD;   // 0x66CD is:  0110 0110 1100 1101
   y = 0xF;      // 0xF is:     0000 0000 0000 1111
   z = x & y;    // z is:       0000 0000 0000 1101


The | operator performs a bitwise logical OR of its' two int operands. 

   int x, y, z;

   x = 0x66CD;   // 0x66CD is:  0110 0110 1100 1101
   y = 0xF;      // 0xF is:     0000 0000 0000 1111
   z = x | y;    // z is:       0110 0110 1100 1111


The ^ operator performs a bitwise logical XOR of its' two int operands. 

   int x, y, z;

   x = 0x66CD;      // 0x66CD is:    0110 0110 1100 1101
   y = 0xF;         // 0xF is:       0000 0000 0000 1111
   z = x ^ y;       // z is:         0110 0110 1100 0010


The << operator shifts bits left, filling with zeros on the right.

   int x, y;

   x = 0x26CD46EC;      // 0x26CD46EC is:    00100110110011010100011011101100
   y = x<<3             // y is:             00110110011010100011011101100000


The >> operator shifts bits right, propagating the (sign) bit from the left

   int x, y;

   x = 0x26CD46EC;      // 0x26CD46EC is:    00100110110011010100011011101100
   y = x>>3             // y is:             00000100110110011010100011011101

   // Suppose x contained:                   11100110110011010100011011101100
   y = x>>3             // y would be:       11111100110110011010100011011101


The >>> operator shifts bits right, filling with zeros on the left.

   int x, y;

   x = 0x26CD46EC;      // 0x26CD46EC is:    00100110110011010100011011101100
   y = x>>>3            // y is:             00000100110110011010100011011101

   // Suppose x contained:                   11100110110011010100011011101100
   y = x>>>3            // y would be:       00011100110110011010100011011101


Note that shifting a positive value left one bit is equivalent to multiplying
it by 2, while shifting a positive value right one bit is equivalent to
dividing it by 2.


Note that the & operator can be used to select a bit.

   int x, bit3, bit5;

   x = 0x66CD;              // 0x66CD is:    0110 0110 1100 1101

   bit3 = x & 0x4;          // 0x4 is:       0000 0000 0000 0100
                            // bit3 is:      0000 0000 0000 0100, i.e., non-zero

   bit5 = x & 0x10;         // 0x10 is:      0000 0000 0001 0000
                            // bit5 is:      0000 0000 0000 0000, i.e., zero

   *Note that 0x4 and 0x10 are sometimes called a "mask."


Note that the | operator can be used to set a bit.

   int x, bit3, bit5;

   x = 0x66CD;              // 0x66CD is:     0110 0110 1100 1101

   x = x | 0x2;             // 0x2 is:        0000 0000 0000 0010
                            // x becomes:     0110 0110 1100 1111

   x = 0x66CD;              // 0x66CD is:     0110 0110 1100 1101

   x = x | 0x4;             // 0x4 is:        0000 0000 0000 0100
                            // x becomes:     0110 0110 1100 1101

