Bitwise operators perform operations on bit level. For example, a bitwise & (AND) operator on two numbers x & y would convert these numbers to their binary equivalent and then perform the logical AND operation on them. C language supports following Bitwise operators:
Bitwise Operators Truth Table:
1. Bitwise & (AND) operator
In the Bitwise & operation, the resultant bit is 1 if the corresponding bits in both the operands is 1. If corresponding bit in any of the operand is 0, the output bit is 0.
For example: The output of (101 & 111) would be 101 because:
First bit in both the operands is 1 so the first output bit is 1
Second bit is 0 in first operand so the output bit is 0.
Third bit is 1 in both the operands so the third output bit is 1.
Let’s take another example and write a C program for the same to verify the output:
12 = 00001100 (In Binary) 18 = 00010010 (In Binary) Bitwise AND Operation of numbers 12 and 18 is: 00001100 & 00010010 ________ 00000000 = 0 (In decimal)
C Program:
#include <stdio.h> int main () { int num1 = 12, num2 = 18; //Bitwise & Operation: num1 & num2 printf("Output of num1 & num2: %d", (num1 & num2)); return 0; }
Output:
Output of num1 & num2: 0
2. Bitwise | (OR) operator
In the Bitwise | (OR) operation, the resultant bit is 0 if the corresponding bits in both the operands is 0. If corresponding bit in any of the operand is 1, the output bit is 1.
For example: The output of (101 & 111) would be 111 because:
First bit in both the operands is 1 so the first output bit is 1
Second bit is 1 in the second operand so the output bit is 1.
Third bit is 1 in both the operands so the third output bit is 1.
Let’s take another example for OR operator and write a C program for the same:
12 = 00001100 (In Binary) 18 = 00010010 (In Binary) Bitwise OR Operation of numbers 12 and 18 is: 00001100 | 00010010 ________ 00011110 = 30 (In decimal)
C Program:
#include <stdio.h> int main () { int num1 = 12, num2 = 18; //Bitwise | (OR) Operation: num1 | num2 printf("Output of num1 | num2: %d", (num1 | num2)); return 0; }
Output:
Output of num1 | num2: 30
3. Bitwise ^ (XOR) operator
Bitwise ^ (XOR) operator is also known as exclusive OR. The output bit of Bitwise XOR is 1 if the corresponding bits of two operands are opposite and the output bit is 0 if the corresponding bits are same.
For example: The output of (101 & 111) would be 010 because:
First bit is same in both the operands so the first output bit is 0.
Second bit is different in both the operands so the output bit is 1.
Third bit is same in both the operands so the third output bit is 0.
Let’s take another example for ^ operator and write a C program for the same:
12 = 00001100 (In Binary) 18 = 00010010 (In Binary) Bitwise XOR Operation of numbers 12 and 18 is: 00001100 ^ 00010010 ________ 00011110 = 30 (In decimal)
C Program:
#include <stdio.h> int main () { int num1 = 12, num2 = 18; //Bitwise ^ (XOR) Operation: num1 ^ num2 printf("Output of num1 ^ num2: %d", (num1 ^ num2)); return 0; }
Output:
Output of num1 ^ num2: 30
4. Bitwise ~ (Complement) operator
Bitwise complement is a unary operator . It changes the 1’s bits to 0’s and 0’s bits to 1’s. It is denoted by ~ (tilde) symbol.
Let’s see, how to find a bitwise complement of a number:
32 = 00100000 (In Binary)
Bitwise complement(~) of number 32: As per the complement rules, 1’s are changed to 0’s and 0’s are changed to 1’s.
~ 00100000
________
11011111 = 223 (In decimal)
However, the compiler prints the 2’s complement of this number.
2's complement of 223 = 1's complement of 223 + 1 = 1's of (11011111) + 1 = 00100000 + 1 = 00100001 = -33 (2's complement are represented by negative sign)
C Program:
#include <stdio.h> int main () { int num = 32; //printing bitwise complement of 32 printf("Bitwise complement of 32 is: %d",(~num)); return 0; }
Output:
Bitwise complement of 32 is: -33
5. Bitwise Shift operators
Bitwise shift operators are of two types:
- Left Shift Operator <<
- Right Shift Operator >>
Left Shift: Left shift operator is denoted by << symbol. It shifts all bits towards left by a certain number of specified bits, for example: num<<2 will shift the bits to the left by two positions. The bit positions that have been vacated by the left shift operator are filled with 0’s.
27 = 00011011 (In binary) 27<<2 = 01101100 (In binary) [Left shift by two bits] = 108 (In decimal)
Right Shift: Right shift operator is denoted by >> symbol. It shifts all bits towards right by certain number of specified bits. For example: num>>2 will shift the bits to the right by two positions. The bit positions that have been vacated by the right shift operator are filled with 0’s.
27 = 00011011 (In binary) 27>>2 = 00000110 (In binary) [Right shift by two bits] = 6 (In decimal)
C Program:
#include <stdio.h> int main () { int number = 27; printf("number is: %d\n", number); //Bitwise left shift <<, shifting the bits to the left //by two positions printf("number << 2: %d\n", (number<<2)); //Bitwise right shift >>, shifting the bits to the right //by two positions printf("number >> 2: %d", (number>>2)); return 0; }
Output: