Contents
What is Bitwise Operator in C?
The bitwise operators are the operators used to carry out the operations at the records at the bit-level. while we perform the bitwise operations, then it’s also referred to as bit-level programming. It consists of digits, both zero or 1. it’s miles particularly utilized in numerical computations to make the calculations quicker.
we have one of a kind kinds of bitwise operators inside the c programming language. the subsequent is the list of the bitwise operators:

Let’s look at the truth table of the bitwise operators.

-
Bitwise AND (&):
Performs bitwise AND operation in the combination of two operations. If both products are 1, the result is 1, otherwise 0.
Example
- #include <stdio.h>
- int main() {
- int a = 5; // Binary: 00000101
- int b = 3; // Binary: 00000011
- int result = a & b; // Result: 00000001 (1)
- printf(“Bitwise AND: %d\n”, result);
- return 0;
- }
Output
Bitwise AND: 1
-
Bitwise OR (|):
Performs a bit OR operation on the combination of two operators. If at least one bit is 1, the result is 1, otherwise 0.
Example
- #include <stdio.h>
- int main() {
- int a = 5; // Binary: 00000101
- int b = 3; // Binary: 00000011
- int result = a | b; // Result: 00000111 (7)
- printf(“Bitwise OR: %d\n”, result);
- return 0;
- }
Output
Bitwise OR: 7
-
Bitwise XOR (^):
Performs a special bitwise OR (XOR) operation on the sum of two operators. The result is 1 if the objects are different, 0 otherwise.
Example
- #include <stdio.h>
- int main() {
- int a = 5; // Binary: 00000101
- int b = 3; // Binary: 00000011
- int result = a ^ b; // Result: 00000110 (6)
- printf(“Bitwise XOR: %d\n”, result);
- return 0;
- }
Output
Bitwise XOR: 6
-
Bitwise Not (~):
Perform NOT bit (completion) of all bit operations. Flips each bit, changing 1 to 0 and 0 to 1.
Example
- #include <stdio.h>
- int main() {
- int a = 5; // Binary: 00000101
- int result = ~a; // Result: 11111010 (-6)
- printf(“Bitwise NOT: %d\n”, result);
- return 0;
- }
Output
Bitwise NOT: -6
-
Left shift (<<):
Shifts all bits in the left operand to the left by the operation number. The empty position on the right is filled with zero.
Example
- #include <stdio.h>
- int main() {
- int a = 5; // Binary: 00000101
- int result = a << 2; // Result: 00010100 (20)
- printf(“Left Shift: %d\n”, result);
- return 0;
- }
Output
Left Shift: 20
-
Right shift (>>):
Shifts all bits in the left operand to the right by the operation number. The empty bits on the left are written as signed bits (for signed numbers) or zeros (for unsigned numbers).
Example
- #include <stdio.h>
- int main() {
- int a = 5; // Binary: 00000101
- int result = a >> 2; // Result: 00000001 (1)
- printf(“Right Shift: %d\n”, result);
- return 0;
- }
Output
Right Shift: 1
Examples
#include <stdio.h>
int main() {
int x = 5;Â Â Â // Binary representation: 00000101
int y = 3;Â Â Â // Binary representation: 00000011
// Bitwise AND
printf(“Bitwise AND: %d\n”, x & y);Â // Output: 00000001 (1)
// Bitwise OR
printf(“Bitwise OR: %d\n”, x | y);Â Â // Output: 00000111 (7)
// Bitwise XOR
printf(“Bitwise XOR: %d\n”, x ^ y);Â // Output: 00000110 (6)
// Bitwise NOT
printf(“Bitwise NOT of x: %d\n”, ~x);Â // Output: 11111010 (-6)
// Left Shift
printf(“Left Shift of x by 2 positions: %d\n”, x << 2);Â // Output: 00010100 (20)
// Right Shift
printf(“Right Shift of x by 2 positions: %d\n”, x >> 2); // Output: 00000001 (1)
return 0;
}
Output
Bitwise AND: 1
Bitwise OR: 7
Bitwise XOR: 6
Bitwise NOT of x: -6
Left Shift of x by 2 positions: 20
Right Shift of x by 2 positions: 1
Explain
- Bitwise AND of 5 and 3 results in 1 (00000101 & 00000011 = 00000001).
- Bitwise OR of 5 and 3 results in 7 (00000101 | 00000011 = 00000111).
- Bitwise XOR of 5 and 3 results in 6 (00000101 ^ 00000011 = 00000110).
- Bitwise NOT of 5 results in -6 ( ~00000101 = 11111010).
- Left shifting 5 by 2 positions results in 20 (00000101 << 2 = 00010100).
- Right shifting 5 by 2 positions results in 1 (00000101 >> 2 = 00000001)