C bitwise operators, XOR in C, XOR C++, C++ bitwise operators, Right shift operator
C provides 6 bitwise operators. Bitwise operators operate on the individual bits in the operand numeric or char value. A bit pattern consists of 0s and 1s. These bitwise operators may be applied only to the char and integer operands. Bitwise operators may not be applied on the other data types like float, double or void. The following table shows the name and meaning of the bitwise operators.
Before understanding the C bitwise operators, let us understand the operations in the mathematical or logical sense.
To put it simply:
AND table
1 and 1 is 1
1 and 0 is 0
0 and 1 is 0
0 and 0 is 0
OR table
1 or 1 is 1
1 or 0 is 1
0 or 1 is 1
0 or 0 is 0
XOR table
1 xor 1 is 0
1 xor 0 is 1
0 xor 1 is 1
0 xor 0 is 0
NOT table
NOT 1 is 0
NOT 0 is 1
Operators | Meaning |
---|---|
>> | Right Shift |
<< | Left Shift |
^ | Bitwise XOR (Exclusive OR) |
~ | One's complement(NOT) |
& | Bitwise AND |
| | Bitwise OR |
If we want to compare two decimal values by using the bitwise operator, then those decimal values should be first converted into binary form and then the bitwise comparison will be taking place.
Bitwise AND, OR, XOR and NOT
The &, |, ^ and ~ operators can perform boolean logical operations. The NOT (~) operator is a unary which can simply perform the conversion that is 0 bits into 1 bits and vice-versa. The other 3 operators are the binary operators. Those binary operators can follow some rules to perform the boolean logical operations. The following is the truth table of AND(&), OR(|) and XOR(^):
A | B | A&B | A|B | A^B |
---|---|---|---|---|
0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 | 1 |
1 | 0 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 |
For example:
Consider the following 8-bit example which have two integer values:
A= 10, and B =8.
The binary form of these values are given below.
A=00001010 B=00001000
Now we have to apply the bitwise operations on them.
Bitwise AND (&):Bitwise AND (&) operator sets the bit to 1 if both corresponding bits in A and B are one.
A & B = 00001010 & 00001000 00001010 00001000 Result: 00001000 (Decimal value is 8)Bitwise OR (|):
| operator sets the bit to 1 if either or both corresponding bits in A and B are one.
A | B = 00001010 | 00001000 00001010 00001000 Result: 00001010 (decimal values is 10)Bitwise XOR (^):
Bitwise XOR (^) operator sets the bit to 1 if one of the 2 corresponding bits, but not both are 1. That is, if one and only one of corresponding bits in A and B are 1.
A^B = 00001010 ^ 00001000 00001010 00001000 Result: 00000010 (decimal value is 2)Bitwise NOT (~):
Bitwise NOT (~) operator performs one's complement that means, convert a 0 bit to 1 and a 1 bit to 0.
A= 00001010 ~A= 11110101 ----(decimal value is 245)
Bitwise Shift operators
The bitwise operators shift the bits of an integer variable to the left or right respectively. The C supports two kinds of shift operators.
- Left Shift
- Right Shift
Bitwise Left shift
Syntax:
x<<n
Here the bits in x are shifted n bit position to the left and the vacated bits are being filled with zeros.
For example:
int x= 4;
/* equal binary values of 4 is 00000100 */
int y=x<<2;
/* equal binary values left shift is 00010000 (decimal 16 is assigned into y ) */
Left shift can use the form to get the output that is input number is multiplied by 2s
Output(Y) = number(n) * 2s
where
s = Number of position to be shifted
n = Input number
Y = Output number
Bitwise Right Shift
Syntax:
x>>n
Here the bits in x are shifted n bit position to right and the vacated bits are being filled with zero.
For example:
int x=8;
/* equal binary value is 00001000 */
int y=x>>2;
/* equal right shifted value
is 00000010 (decimal 2)*/
Therefore, the right shift is done by using input number is to be divided by 2s
Output(Y) = Input number(n) / 2s
where
s = Number of position to be shifted
n = Input number
Y = Output number
Bitwise Operators Example - AND, OR, XOR, NOT, Left Shift, Right Shift
Following is an example C Program using Bitwise Operators.
#include <stdio.h> #include <conio.h> int main() { int A=5; /*binary equal 00000101*/ int B=3; /*binary equal 00000011 */ int C; /*Bitwise AND */ C = A&B; printf("The Bitwise AND is :%d \n",C); /*Bitwise OR */ C = A|B; printf("The Bitwise OR is :%d \n",C); /*Bitwise XOR */ C = A^B; printf("The Bitwise XOR is : %d \n ",C); /*Bitwise NOT */ C = ~A; printf("The bitwise NOT is : %d \n ",C); /*Bitwise Left Shift */ C = A<<2; printf("The Bitwise Left Shift is :%d \n ",C); /*Bitwise Right Shift*/ C = A>>2; printf("The Bitwise Right Shift is :%d \n ",C); return(0); }
Output:
The Bitwise AND is : 1 The Bitwise OR is : 7 The Bitwise XOR is : 6 The Bitwise NOT is : 250 The Bitwise Left shift is : 20 The Bitwise Right Shift is : 1