Bitwise Operators Cheat Sheet
Bitwise operators work directly on binary representations of integers.
They are fundamental in systems programming, cryptography, networking, embedded systems, performance optimizations, and low-level data manipulation.
Binary Basics
| Decimal | Binary |
|---|---|
| 0 | 0000 |
| 1 | 0001 |
| 2 | 0010 |
| 3 | 0011 |
| 4 | 0100 |
| 5 | 0101 |
| 8 | 1000 |
Common Bitwise Operators
| Operator | Name | Description |
|---|---|---|
& |
AND | Sets bit if both bits are 1 |
| |
OR | Sets bit if either bit is 1 |
^ |
XOR | Sets bit if bits differ |
~ |
NOT | Inverts all bits |
<< |
Left Shift | Shifts bits left |
>> |
Right Shift | Shifts bits right |
>>> |
Unsigned Right Shift | Zero-fill right shift (language-specific) |
Bitwise AND (&)
0101 (5)
0011 (3)
----
0001 (1)
5 & 3 // 1
Use cases: - Masking bits - Checking flags - Clearing specific bits
Bitwise OR (|)
0101 (5)
0011 (3)
----
0111 (7)
5 | 3 // 7
Use cases: - Setting bits - Combining flags
Bitwise XOR (^)
0101 (5)
0011 (3)
----
0110 (6)
5 ^ 3 // 6
Properties:
- a ^ a = 0
- a ^ 0 = a
Use cases: - Toggling bits - Swap values without temp variable - Simple encryption
Bitwise NOT (~)
~0101 = 1010
~5 // -6 (two's complement)
Important: - Result depends on integer width - Uses two’s complement representation
Left Shift (<<)
0010 << 1 = 0100
2 << 1 // 4
Rules:
- Shifting left by n ≈ multiply by 2^n
- Bits shifted out are discarded
- Zeros added on the right
Right Shift (>>)
0100 >> 1 = 0010
4 >> 1 // 2
Notes:
- Signed shift preserves sign bit
- Equivalent to dividing by 2^n (floor)
Unsigned Right Shift (>>>)
11111100 >>> 2 = 00111111
- Fills with zeros
- Available in languages like Java, JavaScript
- Useful for unsigned arithmetic
Bit Masks
Creating a mask
int mask = 1 << 3; // 00001000
Check a bit
if (value & mask) {
// bit is set
}
Set a bit
value |= mask;
Clear a bit
value &= ~mask;
Toggle a bit
value ^= mask;
Flags Example
#define READ 1 << 0
#define WRITE 1 << 1
#define EXEC 1 << 2
int perm = READ | WRITE;
| Flag | Binary |
|---|---|
| READ | 001 |
| WRITE | 010 |
| EXEC | 100 |
Swapping Values with XOR
a ^= b;
b ^= a;
a ^= b;
⚠️ Avoid if readability matters — modern compilers optimize swaps.
Checking Even / Odd
if (n & 1) {
// odd
}
Clearing Lowest Set Bit
n &= (n - 1);
Used in: - Bit counting - Performance-critical loops
Counting Set Bits (Hamming Weight)
while (n) {
count++;
n &= (n - 1);
}
Bitwise vs Logical Operators
| Bitwise | Logical |
|---|---|
& |
&& |
| |
|| |
| Operates on bits | Operates on booleans |
| No short-circuit | Short-circuit |
Common Pitfalls
- Mixing signed and unsigned shifts
- Assuming integer size
- Forgetting operator precedence
- Using bitwise ops on floats
When to Use Bitwise Operations
- Low-level system code
- Performance-critical paths
- Memory optimization
- Protocol parsing
- Cryptography primitives
Summary
- Bitwise operators manipulate individual bits
- Extremely fast and memory-efficient
- Require careful usage for readability and correctness
- Essential knowledge for systems & security programming