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
| 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 |