Introduction
Bitwise operations are low-level computational operations that directly manipulate bits—the fundamental units of data in digital systems. Unlike arithmetic operations that treat values holistically, bitwise operations work at the bit level, enabling high-performance computing, compact data representation, and fine-grained control over hardware, logic, and data structures.
Used extensively in systems programming, embedded development, competitive coding, graphics processing, and cryptography, bitwise operations form the core of many optimization techniques.
What Are Bitwise Operations?
A bitwise operation applies a logical or arithmetic transformation to the individual bits of one or more binary numbers. These operations are defined for fixed-width integers and typically operate on their binary representations.
Common Bitwise Operators
| Operation | Symbol | Description |
|---|---|---|
| AND | & | Sets bit to 1 if both corresponding bits are 1 |
| OR | ` | ` |
| XOR | ^ | Sets bit to 1 if bits are different |
| NOT (complement) | ~ | Flips each bit |
| Left Shift | << | Shifts bits to the left |
| Right Shift | >> | Shifts bits to the right |
Bitwise AND (&)
Logic:
1 & 1 = 1
1 & 0 = 0
0 & 1 = 0
0 & 0 = 0
Usage:
- Clearing bits
- Masking values
- Range checking
a = 12 # 1100
b = 5 # 0101
result = a & b # 0100 → 4
Bitwise OR (|)
Logic:
1 | 1 = 1
1 | 0 = 1
0 | 1 = 1
0 | 0 = 0
Usage:
- Setting bits
- Combining flags
a = 12 # 1100
b = 5 # 0101
result = a | b # 1101 → 13
Bitwise XOR (^)
Logic:
1 ^ 1 = 0
1 ^ 0 = 1
0 ^ 1 = 1
0 ^ 0 = 0
Usage:
- Toggling bits
- Swapping values without temp variable
- Finding non-duplicate in arrays
a = 12 # 1100
b = 5 # 0101
result = a ^ b # 1001 → 9
Bitwise NOT (~)
Logic:
~x = -x - 1
Inverts all bits.
x = 5 # 0000 0101
result = ~x # 1111 1010 (in 8-bit: -6)
Note:
- Result depends on the number of bits considered
- Python uses infinite-precision integers, so it returns the two’s complement
Left Shift (<<)
Shifts bits to the left, equivalent to multiplying by 2:
x = 3 # 0011
result = x << 1 # 0110 → 6
Each shift multiplies by 2:
x << n = x * 2^n
Right Shift (>>)
Shifts bits to the right, equivalent to integer division by 2:
x = 12 # 1100
result = x >> 2 # 0011 → 3
Each shift divides by 2:
x >> n = x // 2^n
Bitwise Operations in Python
a = 29 # 00011101
b = 15 # 00001111
print(a & b) # 13
print(a | b) # 31
print(a ^ b) # 18
print(~a) # -30
print(a << 1) # 58
print(a >> 2) # 7
Bitwise Operations in C/C++
unsigned int a = 29; // 00011101
unsigned int b = 15; // 00001111
int and_op = a & b; // 13
int or_op = a | b; // 31
int xor_op = a ^ b; // 18
int not_op = ~a; // Depends on type width
Bitwise Operations in JavaScript
let a = 29; // 00011101
let b = 15; // 00001111
console.log(a & b); // 13
console.log(a | b); // 31
console.log(a ^ b); // 18
console.log(~a); // -30
Applications of Bitwise Operations
1. Flag Management
READ = 1 << 0 # 0001
WRITE = 1 << 1 # 0010
EXECUTE = 1 << 2 # 0100
permissions = READ | WRITE
if permissions & READ:
print("Read allowed")
2. Subset Enumeration
arr = [1, 2, 3]
n = len(arr)
for i in range(1 << n):
subset = [arr[j] for j in range(n) if i & (1 << j)]
print(subset)
3. Bit Counting (Brian Kernighan’s Algorithm)
def count_ones(x):
count = 0
while x:
x &= x - 1
count += 1
return count
4. Checking Power of Two
def is_power_of_two(x):
return x > 0 and (x & (x - 1)) == 0
5. Swapping Without Temp Variable
a, b = 5, 9
a ^= b
b ^= a
a ^= b
Bitwise Masks
Use masks to select or isolate bits:
mask = 0b11110000
value = 0b10101100
result = value & mask # 10100000
Performance Benefits
- Bitwise operations are constant time (O(1))
- Compiled into single CPU instructions
- Extremely useful for memory-constrained systems and real-time applications
Caution and Common Pitfalls
| Issue | Example | Fix |
|---|---|---|
| Signed/Unsigned confusion | Right shifts on signed ints | Use unsigned types if logical shift needed |
| Not masking before checks | x & 3 == 1 | Use parentheses: (x & 3) == 1 |
| Bit overflow | Left shifting beyond type width | Ensure correct data width |
| Counting from wrong end | Bit 0 is LSB | Start counting from right to left |
Summary
Bitwise operations provide a high-speed and resource-efficient way to manipulate and analyze binary data. Whether you’re working with flags, encoding values compactly, performing mathematical tricks, or implementing low-level protocols, understanding bitwise logic enables more expressive and powerful programming.
Mastering bitwise operations is essential not just for systems developers, but also for algorithm engineers and data compression specialists who require fine control over binary information.
Related Keywords
- Bit Mask
- Binary Representation
- Bitwise AND
- Bitwise Left Shift
- Bitwise NOT
- Bitwise OR
- Bitwise Right Shift
- Bitwise XOR
- Boolean Algebra
- Brian Kernighan Algorithm
- Flag Management
- Logical Shift
- Power Of Two Check
- Subset Enumeration
- Two’s Complement









