C Bitwise Operators
What are Bitwise Operators in C?
Bitwise Operators in C are operators that perform operations directly on the binary representation of integers.
These operators compare and manipulate individual bits instead of whole numbers.
Bitwise operators are commonly used in embedded systems, networking, device drivers, encryption, and performance optimization.
Example
int a = 5;
int b = 3;
int result = a & b;
Explanation
• a contains the value 5 (Binary: 0101).
• b contains the value 3 (Binary: 0011).
• & is the Bitwise AND operator.
• The operation compares each corresponding bit.
• The result is stored in the variable result.
Why are Bitwise Operators Important?
Bitwise Operators are important because they:
• Perform fast binary operations.
• Manipulate individual bits efficiently.
• Improve program performance.
• Are widely used in low-level programming.
• Help control hardware and optimize memory usage.
Types of Bitwise Operators in C
C provides several bitwise operators to perform operations on individual bits of integer values.
The most commonly used bitwise operators are:
• Bitwise AND (&)
• Bitwise OR (|)
• Bitwise XOR (^)
• Left Shift (<<)
• Right Shift (>>)
Each operator performs a different binary operation.
Bitwise AND (&)
The Bitwise AND (&) operator compares each corresponding bit of two numbers.
The result is 1 only if both bits are 1; otherwise, the result is 0.
Example
int a = 5;
int b = 3;
int result = a & b;
Explanation
• a = 5 (Binary: 0101)
• b = 3 (Binary: 0011)
• The AND operation compares each bit.
• Result = 0001 (Decimal: 1)
• The value stored in result is 1.
Bitwise OR (|)
The Bitwise OR (|) operator compares each corresponding bit of two numbers.
The result is 1 if at least one of the bits is 1.
Example
int a = 5;
int b = 3;
int result = a | b;
Explanation
• a = 5 (Binary: 0101)
• b = 3 (Binary: 0011)
• The OR operation compares each bit.
• Result = 0111 (Decimal: 7)
• The value stored in result is 7.
Bitwise XOR (^)
The Bitwise XOR (^) operator compares each corresponding bit of two numbers.
The result is 1 only when the two bits are different.
Example
int a = 5;
int b = 3;
int result = a ^ b;
Explanation
• a = 5 (Binary: 0101)
• b = 3 (Binary: 0011)
• XOR returns 1 when bits are different.
• Result = 0110 (Decimal: 6)
• The value stored in result is 6.
Left Shift (<>)
The Left Shift (<<) operator shifts bits to the left, while the Right Shift (>>) operator shifts bits to the right.
Shifting bits changes the binary value and is commonly used for fast multiplication and division by powers of 2.
Example
int a = 5;
int left = a << 1;
int right = a >> 1;
Explanation
• a = 5 (Binary: 0101)
• a << 1 shifts all bits one position to the left.
• a >> 1 shifts all bits one position to the right.
• Left shift generally multiplies by 2.
• Right shift generally divides by 2 for positive integers.
Advantages of Bitwise Operators
• Perform operations faster than many arithmetic operations.
• Manipulate individual bits efficiently.
• Improve program performance.
• Reduce memory usage in low-level applications.
• Widely used in embedded systems, networking, and device programming.
Example Program
Program
#include <stdio.h>
int main()
{
int a = 5;
int b = 3;
printf(“AND = %d\n”, a & b);
printf(“OR = %d\n”, a | b);
printf(“XOR = %d\n”, a ^ b);
printf(“Left Shift = %d\n”, a << 1);
printf(“Right Shift = %d\n”, a >> 1);
return 0;
}
Output
AND = 1
OR = 7
XOR = 6
Left Shift = 10
Right Shift = 2
Explanation
• Bitwise AND (&) returns 1.
• Bitwise OR (|) returns 7.
• Bitwise XOR (^) returns 6.
• Left Shift (<<) shifts bits to the left and doubles the value.
• Right Shift (>>) shifts bits to the right and halves the value for positive integers.
• Bitwise operators work directly on binary values.
Key Points
• Bitwise operators work on binary data.
• Common operators are &, |, ^, <<, and >>.
• They perform fast low-level operations.
• They are widely used in embedded systems and hardware programming.
• Left Shift generally multiplies by 2.
• Right Shift generally divides by 2 for positive integers.
• Bitwise operators improve performance in many applications.
