Assignment Question
Study the Bitwise Operators and Work Out Five (5) Programming Examples
demonstrating your acquired knowledge.
Bitwise Operators
Bitwise operators perform operations directly on the individual bits of an integer.
All the operators are binary, except the One’s complement (~) operator, which is unary.
The bitwise operators are as follows with their meaning and examples.
S/No Bitwise operators Meaning
1. & Bitwise AND
2. | Bitwise OR
3. ^ Bitwise XOR
4. ~ Bitwise NOT
5. << Left Shift
6. >> Right Shift
1. Bitwise AND (&)
It is a binary operator and requires two operands. These operands are compared bitwise i.e. all
the corresponding bits in both operands are compared. The resulting bit is 1, only when the bits
in both operands are 1, otherwise it is 0.
#include <stdio.h>
int main() {
int a = 5; // Binary: 0101
int b = 3; // Binary: 0011
int result = a & b; // Binary: 0001 (Decimal: 1)
printf("Result of AND: %d\n", result); // Output: 1
return 0;
}
2. Bitwise OR (|)
It is a binary operator and requires two operands. These operands are compared bitwise i.e. all
the corresponding bits in both operands are compared. The resulting bit is 0, only when the bits
in both operands are 0, otherwise it is 1.
#include <stdio.h
int main() {
int a = 5; // Binary: 0101
int b = 3; // Binary: 0011
int result = a | b; // Binary: 0111 (Decimal: 7)
printf("Result of OR: %d\n", result); // Output: 7
return 0;
}
3. Bitwise XOR (^)
It is a binary operator and requires two operands. The corresponding bits of both operands are
compared and the resulting bit is 1, if bits of both operands have different value, otherwise it is 0.
#include <stdio.h>
int main() {
int a = 5; // Binary: 0101
int b = 3; // Binary: 0011
int result = a ^ b; // Binary: 0110 (Decimal: 6)
printf("Result of XOR: %d\n", result); // Output: 6
return 0;
}
4. Bitwise NOT (~)
It is a unary operator and requires only one operand. It negates the value of the bit. If the bit of
the operand is 1 then the resulting bit is 0 and if the bit of the operand is 0 then the resulting bit is
1.
C
#include <stdio.h>
int main() {
int a = 5; // Binary (assuming 8-bit): 0000 0101
int result = ~a; // Binary (two's complement): 1111 1010 (Decimal: -6)
printf("Result of NOT: %d\n", result); // Output: -6
return 0;
}
5. Left Shift (<<)
This operator is used for shifting the bits left. It requires two operands. The left operand is the
operand whose bits are shifted and the right operand indicates the number of bits to be shifted.
On shifting the bits left, an equal number of bit positions on the right are vacated. These
positions are filled in with 0 bits.
#include <stdio.h>
int main() {
int a = 5; // Binary: 0101
int result = a << 2; // Binary: 010100 (Decimal: 20)
printf("Result of Left Shift: %d\n", result); // Output: 20
return 0;
}
6. Right Shift (>>)
This operator is similar to the left shift operator, except that it shifts the bits to the right side.
On shifting the bits right, an equal number of bit positions on the left are vacated. These
positions are filled in with 0 bits in unsigned integers.
#include <stdio.h>
int main() {
int a = 20; // Binary: 010100
int result = a >> 2; // Binary: 000101 (Decimal: 5)
printf("Result of Right Shift: %d\n", result); // Output: 5
return 0;
}