Read: § 2.9 of the C Programming Language, Bitwise Operations pp 45-6

Binary Operations

Bitwise operations are operations that apply to the bits in integers.
Let a and b be integers. The operations below give us new integers.
a & b
a | b
a ^ b
~a
The & (and) operation, the | (or) operation, and the ^ (exclusive or) operation apply to corresonding bits in a and b.

The result of & is 0 unless both corresponding bits are 1, then the result is 1.
The result of | is 1 unless both corresponding bits are 0, then the result is 0.
The result of ^ is 0 unless the corresponding bits are different, then the result is 1.
The result of ~ is to make 0 bits into 1 bits and 1 bits into 0 bits.

Example.

a = 18 = 00010010 Binary
b =  6 = 00000110 Binary

a & b =  00000010 Binary =   2
a | b =  00010110 Binary =  22
a ^ b =  00010100 Binary =  20
~a    =  11101101 Binary = 237

Numbering The Bits

Bits are counted from right to left. The right-most bit is bit 0. It is also called the 1's place. Note: 1 is 2 raised to the exponent 0. This continues: the next bit is bit 1. It is also the 2's place and 2 is 2 raised to the exponent 1. The next bit is bit 2. It is also the 4's place and 4 is 2 raised to the exponent 2.

Setting And Clearing Bits

Setting a bit means making the bit a 1. Clearing a bit means making a the bit a 0.

Example.

a = b = 18 = 00010010 Binary
 
a |= 8;   //sets  bit 3 of a; note 8 is 2 raised to exponent 3; a is now 26

b &= (~16); //clears bit 4 of b; Note 16 is 2 raised to exponent 4; b is now 2

Shifts

There are two shifts. Format:
  expression >> shift_amount
  expression << shift_amount
Operation >> is a shift right of the bits of the expression.
Example:
a = 18 = 00010010 Binary
a >> 2
The value is the value of the expression but shifted two bits to the right. The two right-most bits are lost; Two new 0 bits come in on the left
a >> 2 = 00000100 Binary = 4
Operation << is a shift left of the bits of the expression.
Example:
a = 18 = 00010010 Binary
a << 4
The value is the value of the expresssion but shifted 4 bits left. 4 0's are brought in on the right and the left-most 4 bits are lost.
a << 4 = 00100000  Binary = 32
next: Boolean Expressions