a & b a | b a ^ b ~aThe & (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
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
expression >> shift_amount expression << shift_amountOperation >> is a shift right of the bits of the expression.
a = 18 = 00010010 Binary a >> 2The 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 = 4Operation << is a shift left of the bits of the expression.
a = 18 = 00010010 Binary a << 4The 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 = 32next: Boolean Expressions