## C Bitwise Operators

In C programming language the data manipulation can be done on the bit level as well. In C programming language this is done through bitwise operators below a list of bitwise operators is given.

### Bitwise AND Operator (&)

This is a binary operator and used to manipulate each individual byte of an operand. Since it is a binary operator so it requires a minimum of two operands to be performed any execution. Each and every corresponding byte of both operands are compared and the resultant would be either 1 or 0. If the corresponding bits of both operands are 1 then the result will be 1 otherwise the results will be zero.

Let ‘a’ and ‘b’ are two integer variables with their respective values as a = 0x1A2B and b = 0x3C6F. Their manipulation with the help of bitwise AND operator will be as follow.

a = 0001 1010 0010 1011
b = 0011 1100 0110 1111
a&b = 0001 1000 0010 1011
The value of a&b is = 0x182B.

### Bitwise OR Operator (|)

This is another binary operator. Each and every corresponding byte of both operands are compared and the resultant would be either 1 or 0. If corresponding bits of both operands are 0 then the result will be 0 otherwise the results will be 1. Let a = 0x1A2B and b = 0x3C6F. The manipulation with the help of the bitwise OR operator will be as follow.

a = 0001 1010 0010 1011
b = 0011 1100 0110 1111
a&b = 0011 1110 0110 1111
The value of a&b is = 0x3E6F.

### Bitwise XOR Operator (^)

This is a binary operator. Each and every corresponding byte of both operands are compared and the resultant would be either 1 or 0. If the corresponding bits of both operands are the same then the result will be 0 otherwise the results will be 1. Let a = 0x1A2B and b = 0x3C6F. The manipulation with the help of the bitwise OR operator will be as follow.

a = 0001 1010 0010 1011
b = 0011 1100 0110 1111
a&b = 0010 0110 0100 0100
The value of a&b is = 0x2644.

Note: During the manipulation of these operators if the size of operands is different, then the size of the smaller operand will be increased to an exact match with the size of the other operand. In smaller operands, the extra bits will be added to the left. If the smaller operand is unsigned then the extra bits will be zero. If the smaller operand is signed, then the left bits will be filled with the sign bit.

### Bitwise Left Shift Operator (<<)

It is a binary operator requires two operands to manipulate. On the left side, the operand is written whose bits are to be shifted to the left side and the right side operand specifies the number of bits to be shifted. While shifting bits towards left some vacant places are created on right those vacant places are filled with zero. Vacant places will be equal to the number of bits shifted towards left. Let a = 0x1A2B and b = 4. The manipulation (a<<b) of Bitwise Left Shift Operator (<<) will be as follows.

a = 0001 1010 0010 1011 (before manipulation).
a = 1100 0110 1111 0000 (after manipulation).
Lost bits = 0001
Now the value of a is = 0xC6F0.

### Bitwise Right Shift Operator (>>)

It’s working is the same as that of the bitwise left shift operator but it shifts the bits towards the right. Let a = 0x1A2B and b = 4. The manipulation (a>>b) of Bitwise Right Shift Operator (>>) will be as follow.

a = 0001 1010 0010 1011 (before manipulation).
a = 0000 0001 1010 0010 (after manipulation).
Lost bits = 1011.
Now the value of a is = 0x01A2.

Note: While manipulating bitwise right shift operator if the operand is signed integer then the final result will be compiler dependent based on logical shift or arithmetic shift.

### Bitwise One’s Complements Operator (~)

It is a unary operator that requires only one operand to perform any manipulation. It negates the value of the byte means if the byte is 1 so it makes it 0 and vice versa. Let a = 0x1A2B the manipulation (~a) of Bitwise One’s Complements Operator (~) will be as follow.

a = 0001 1010 0010 1011 (before manipulation).
a = 1110 0101 1101 0100 (after manipulation).
Now the value of a is = 0xE5D4.