Bitwise Operators

Bitwise NOT
Reverses each bit in its operand. For example:-
 * ~11001001 = 00110110

Bitwise AND
Compares each bit in its two operands and when two corresponding bits are both 1, the result is 1. If either or both bits are 0, the result is 0. For example:-
 * 11001001 & 10011011 = 10001001

Bitwise OR
Compares each bit in its two operands and when the two corresponding bits are both 0, the result is zero. If either or both bits are 1, the result is 1. For example:-
 * 11001001 | 10011011 = 11011011

Bitwise XOR
Compares each bit in its two operands and when the two corresponding bits are different the result is 1. If the two corresponding bits are the same the result is 0. For example:-
 * 11001001 ^ 10011011 = 01010000

Bitwise left shift
Shifts the bits of the first operand to the left by the number of places given in the second operand. Bits shifted off the left side of the value are discarded. New bits added on the right side will all be zero. For example:-


 * x = 47;   /* 47 is 00101111 in binary */
 * z = x << 1;   /* 00101111 << 1 is 01011110 */

Bitwise right shift
Shifts the bits of the first operand to the right by the number of places given in the second operand. Bits shifted off the right side of the value are discarded. New bits added on the left side are usually zero, but if the first operand is a signed negative value, then the added bits wil either be 0 or whatever value was previously in the leftmost bit position depending on the compiler used. For example:-


 * x = 47;  /* 47 is 00101111 in binary */
 * z = x >> 1;  /* 00101111 >> 1 is 00010111 */

Syntax
z = ~x;

z = x & y; x &= y;    // equivalent to x = x & y

z = x | y; x |= y;    // equivalent to x = x | y

z = x ^ y; x ^= y;    // equivalent to x = x ^ y

z = x << y; x <<= y;   // equivalent to x = x << y

z = x >> y; x >>= y;   // equivalent to x = x >> y