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. Below is a complete table-by-table outline of this topic, spanning foundational concepts through advanced details.
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 |