Bitwise Operators

Bits and Bytes

We know that memory is made up of bits and bytes, and we should know what they are by now. The smallest built-in data type is the char, which on most systems today is 1 byte.
So...  what if we want to access individual bits?  Is this possible?  Yes -- but not directly.  We must use the bitwise operators to act at the bit level.

Caution:  Bitwise operations may be machine-dependent, to an extent, because they rely on the way that the bits are arranged in memory.  Some machines may not store all built-in data types in the same formats.  Most machines, however, tend to use a pretty standard representation for integer types, called "2's Complement" format.
 

The Bitwise Operators

 
Operator Name Arity Description
& bitwise AND binary Similar to the && operator, but on a bit-by-bit basis.  Bits in the result set to 1 if corresponding operand bits are both 1, and set to 0 otherwise
| bitwise OR (inclusive) binary Similar to the || operator, but on a bit-by-bit basis.  Bits in the result set to 1 if at least one of the corresponding bits in the two operands is 1.  0 otherwise.
^ bitwise exclusive OR binary Bits in the result set to 1 if exactly one of the corresponding bits in the two operands is 1.  Result bit set to 0 if both corresponding bits in operands are the same.
<< left shift binary Shifts the bits of the first operand to the left, by the number of bits specified in the second operand.  Right fill with 0 bits.
>> right shift binary Shifts the bits of the first operand to the right, by the number of bits specified in the second operand.  Left fill depends on the machine.  Usually based on the sign (fill with 0's for positive numbers, 1's for negatives).
~ complement unary Flips the bits in the operand.  Similar to negation.  (All 1's become 0's, and all 0's become 1's).

In addition to these, there are also shortcut assignment operators, like with the arithmetic operators.

  x &= y      means x = x & y
  x |= y      means x = x | y
  x ^= y      means x = x ^ y
  x <<= y     means x = x << y
  x >>= y     means x = x >> y

Examples

Suppose we have the following code:
  short x = 6891;
  short y = 11318;
And let's assume that we are using a machine in which a short is 2 bytes (which is 16 bits). The binary representations of x and y, then, are:
     x:  00011010 11101011
     y:  00101100 00110110
Suppose we did the operation (x & y). Then we perform the AND operation on the individual bits:
     x:  00011010 11101011
     y:  00101100 00110110
--------------------------
result:  00001000 00100010    // this is the value 2082

Here is the bitwise OR operation (x | y):
     x:  00011010 11101011
     y:  00101100 00110110
--------------------------
result:  00111110 11111111    // this is the value 16127

Here is the bitwise exclusive OR operation (x ^ y):
     x:  00011010 11101011
     y:  00101100 00110110
--------------------------
result:  00110110 11011101    // this is the value 14045

Here is a bitwise left shift, performed on x (x << 2):
      x:  00011010 11101011
---------------------------
shifted:  01101011 10101100    // this is the value 27564

Here is a bitwise right shift, performed on y (y >> 4):
      y:  00101100 00110110
---------------------------
shifted:  00000010 11000011    // this is the value 707

And here is the complement of x (~x)
      x:  00011010 11101011
---------------------------
     ~x:  11100101 00010100    // this is the value -6892

Code examples