Bitwise operators are the low-level power tools of the C programming language. They allow you to manipulate individual bits of data, offering control and performance advantages that are critical in:
- Embedded systems
- Networking protocols
- Cryptography
- Device drivers
- Memory optimization
If you’re working close to hardware or dealing with flags, bitwise logic is indispensable. This guide explains all bitwise operators in C with syntax, use cases, and examples that actually make sense.
🔧 List of Bitwise Operators in C
Operator | Name | Symbol | Example |
---|---|---|---|
AND | Bitwise AND | & | a & b |
OR | Bitwise OR | | | `a |
XOR | Bitwise XOR | ^ | a ^ b |
NOT | Bitwise NOT | ~ | ~a |
Left Shift | Shift Left | << | a << n |
Right Shift | Shift Right | >> | a >> n |
Let’s explore each operator with real working code.
✅ 1. Bitwise AND (&
)
#include <stdio.h>
int main() {
int a = 12; // 1100 in binary
int b = 10; // 1010 in binary
int result = a & b;
printf("a & b = %d\n", result); // Output: 8 (1000)
return 0;
}
Explanation:
The &
operator compares each bit of two numbers and returns 1 only if both bits are 1. So, 1100 & 1010 = 1000
(which is 8). This is commonly used in bit masking.
✅ 2. Bitwise OR (|
)
int a = 12; // 1100
int b = 10; // 1010
int result = a | b;
printf("a | b = %d\n", result); // Output: 14 (1110)
Explanation:
The |
operator sets a bit if either bit is 1. This is useful for setting bits in a flag or register without affecting other bits.
✅ 3. Bitwise XOR (^
)
int a = 12; // 1100
int b = 10; // 1010
int result = a ^ b;
printf("a ^ b = %d\n", result); // Output: 6 (0110)
Explanation:
The ^
operator sets a bit to 1 if the bits are different. This is commonly used in bit toggling and in encryption algorithms like XOR cipher.
✅ 4. Bitwise NOT (~
)
int a = 12; // 00001100
int result = ~a;
printf("~a = %d\n", result); // Output: -13 (in 2's complement)
Explanation:
The ~
operator inverts every bit. For positive integers, it results in a negative number due to 2’s complement representation. ~12 = -13
because it flips all bits and adds 1 in binary.
✅ 5. Left Shift (<<
)
int a = 5; // 00000101
int result = a << 2;
printf("a << 2 = %d\n", result); // Output: 20 (00010100)
Explanation:
Shifting bits left by 2 places is equivalent to multiplying by 4. So, 5 << 2 = 5 * 4 = 20
. This is often used in fast arithmetic operations.
✅ 6. Right Shift (>>
)
int a = 20; // 00010100
int result = a >> 2;
printf("a >> 2 = %d\n", result); // Output: 5 (00000101)
Explanation:
Shifting bits right by 2 places divides the number by 4 (integer division). This is a fast method of division by powers of 2.
🧪 Real-World Example: Bitmasking Flags
#define FLAG_READ 0x01 // 0001
#define FLAG_WRITE 0x02 // 0010
#define FLAG_EXEC 0x04 // 0100
int permissions = FLAG_READ | FLAG_WRITE; // 0011
if (permissions & FLAG_WRITE) {
printf("Write permission granted.\n");
}
Explanation:
We define different permission flags using bit patterns. Using bitwise OR (|
), we combine permissions. Then, we use bitwise AND (&
) to check if a specific permission is set.
🧠 Use Case: Swapping Variables with XOR (No Temp Variable)
int x = 5, y = 10;
x = x ^ y;
y = x ^ y;
x = x ^ y;
printf("x = %d, y = %d\n", x, y); // Output: x = 10, y = 5
Explanation:
This clever trick uses XOR to swap values without needing an extra variable. It’s fast and memory-efficient, though it’s mostly used in interview questions or low-level optimization.
⚠️ Common Mistakes to Avoid
Mistake | Why It’s Wrong | Fix |
---|---|---|
Confusing & with && | & is bitwise AND, && is logical | Use & only for bit-level ops |
Shifting negative integers | Undefined behavior in C | Avoid using shift on signed types |
Not using unsigned int | Can cause sign issues in NOT/shift ops | Prefer unsigned for bitwise ops |
Bitwise operators give you raw, granular control over data, making your programs fast, compact, and closer to the hardware. Mastering them is a must for systems programmers, embedded developers, and anyone writing performance-critical C code.
Key Takeaways:
- Use AND, OR, and XOR to manipulate bits
- Use SHIFT operators for fast arithmetic
- Use NOT carefully due to sign behavior
- Bitmasking is the foundation of embedded programming and protocol handling
Which bitwise operator do you use the most—and for what task?
Have a tricky bit manipulation problem? Share it in the comments below, and we’ll solve it together! 🔧👇