C Bitwise Operators – Direct Manipulation of Bits
Bitwise operators in C allow you to perform operations directly on binary numbers (bits). They are widely used in low-level programming, embedded systems, and performance optimization.
If you want to work close to hardware and write efficient code, mastering bitwise operators is essential.
What are Bitwise Operators in C?
Bitwise operators work on binary representations of numbers.
Example:
Decimal 5 → Binary 0101
Decimal 3 → Binary 0011
Operations are performed bit by bit.
Why Use Bitwise Operators?
Bitwise operators:
Perform operations faster than arithmetic.
Help in flags, masks, compression, and encryption.
Give low-level control over data and memory.
Types of Bitwise Operators in C
| Operator | Name | Symbol |
|---|---|---|
| AND | Bitwise AND | & |
| OR | Bitwise OR | ` |
| XOR | Bitwise XOR | ^ |
| NOT | Bitwise NOT | ~ |
| Left Shift | Shift Left | << |
| Right Shift | Shift Right | >> |
Bitwise AND (&)
Returns 1 only if both bits are 1.
Example:
#include <stdio.h>
int main() {
int a = 5, b = 3;
printf("%d", a & b);
return 0;
}Output: 1
Binary:
—-
0001
Bitwise OR (|)
Returns 1 if at least one bit is 1.
Example:
int result = 5 | 3;Output: 7
Binary:
—-
0111
Bitwise XOR (^)
Returns 1 if bits are different.
Example:
int result = 5 ^ 3;Output: 6
Binary:
—-
0110
Bitwise NOT (~)
Reverses all bits.
Example:
int a = 5; // 0101
int result = ~a; // 1010 → -6 (in 2's complement for 4 bits)
Left Shift (<<)
Shifts bits to the left (multiply by 2).
Example:
int a = 3; // 0011
int result = a << 2; // 1100 → 12
Right Shift (>>)
Shifts bits to the right (divide by 2).
int a = 12; // 1100
int result = a >> 2; // 0011 → 3
Step-by-Step Guide to Using Bitwise Operators
Step 1: Convert numbers to binary
Step 2: Apply operator
Step 3: Convert result back to decimal
Complete Code Example
#include <stdio.h>
int main() {
int a = 5, b = 3;
printf("a & b = %d\n", a & b); // AND
printf("a | b = %d\n", a | b); // OR
printf("a ^ b = %d\n", a ^ b); // XOR
printf("~a = %d\n", ~a); // NOT
printf("a << 1 = %d\n", a << 1); // Left Shift
printf("a >> 1 = %d\n", a >> 1); // Right Shift
return 0;
}
Output:
a & b = 1
a | b = 7
a ^ b = 6
~a = -6
a << 1 = 10
a >> 1 = 2Real-World Use Cases
Check Even or Odd
if (num & 1)
printf("Odd");
else
printf("Even");Swap Numbers (XOR Trick)
a = a ^ b;
b = a ^ b;
a = a ^ b;Best Practices
| Tip | Why it Matters |
|---|---|
Use with unsigned for clarity | Avoids sign-related issues with shifts |
Use masks with & and ` | ` |
| Avoid unnecessary shifting large values | Can lead to undefined behavior |
Common Mistakes
| Mistake | Problem |
|---|---|
| Using signed int with shifts | May cause unexpected results |
| Forgetting parentheses | Bitwise has lower precedence than comparison |
Misunderstanding ~a | Returns 2’s complement, not just flipped bits |
Learn C Operators to understand conditions.
Also check C If-Else and
C Loops for better logic building.
Comparison Table (Bitwise Operators)
| Operator | Symbol | Description | Example Result |
|---|---|---|---|
| AND | & | Both bits must be 1 | 5 & 3 = 1 |
| OR | | | Any bit is 1 | 5 | 3 = 7 |
| XOR | ^ | Different bits | 5 ^ 3 = 6 |
| NOT | ~ | Reverse bits | ~5 = -6 |
| Left Shift | << | Multiply by 2 | 5 << 1 = 10 |
| Right Shift | >> | Divide by 2 | 5 >> 1 = 2 |
FAQs (Frequently Asked Questions)
1. What are bitwise operators in C?
They perform operations directly on binary bits.
2. What is XOR used for?
For comparing bits and swapping values.
3. Why use bitwise operators?
For performance and low-level programming.
4. What is left shift?
Moves bits left (multiplication).
5. What is right shift?
Moves bits right (division).