Bitwise operations work directly on the binary representation of integers, enabling compact data encoding, high-performance algorithms, and low-level hardware control. Mastering the seven core operators—AND, OR, XOR, NOT, left shift, arithmetic right shift, and logical right shift—unlocks a toolkit of O(1) tricks: toggling flags, isolating bits, counting set bits in O(k), detecting power-of-two values, implementing branchless arithmetic, and solving classic problems (missing number, XOR swap, Gray code). Two's complement representation ties everything together, making signed-integer arithmetic consistent with unsigned bit patterns in nearly every modern language and CPU architecture.
What This Cheat Sheet Covers
This topic spans 12 focused tables and 156 indexed concepts, 98 flashcards. Below is a complete table-by-table outline of this topic, spanning foundational concepts through advanced details.
A jump-to index of every table row in this cheat sheet.
An interactive map of every table and concept in this topic.
Table 1 — Binary Representation Fundamentals
Before any bit trick makes sense you need a firm grasp of how integers are actually laid out in memory. These rows cover the building blocks—unsigned versus two's complement, where the sign bit lives, what -1 and INT_MIN look like in raw bits, and how sign extension, hex notation, and endianness shape the bytes you'll be manipulating.
| Concept | Example | Description | |
|---|---|---|---|
0b1011 = 11 | • Each bit $k$ contributes $2^k$ • value always ≥ 0 | ||
0b0110 = +6 | • MSB = 0 → positive • value = normal binary | ||
0b1010 = −6 (8-bit) | • MSB = 1 → negative • value = −(~n + 1) | ||
~n + 1 | Flip all bits then add 1 | ||
~0b0110 = 0b1001 | • All bits flipped • has +0 and −0 (not used in modern CPUs) | ||
bit 31 in int32 | 0 = positive, 1 = negative | ||
0xFFFFFFFF (32-bit) | • All bits set • universal across widths | ||
0x80000000 (32-bit) | • Only the sign bit set • has no positive counterpart | ||
0x7FFFFFFF (32-bit) | All bits except sign bit set | ||
$-2^{n-1}$ to $2^{n-1}-1$ | e.g., 8-bit: −128 to 127 | ||
$0$ to $2^n - 1$ | e.g., 8-bit: 0 to 255 | ||
0b1010 (4-bit) → 0b11111010 (8-bit) | Replicate MSB into all new upper bits when widening | ||
0xFF = 255 | • Each hex digit = 4 bits • common for masks | ||
0x0A0B in memory | • Little-endian: LSB first • big-endian: MSB first |