Lesson History
6 lessonsBinary Bit Manipulation Tricks: Efficient Extraction and Flag Operations
mediumEvery integer is already an array of independent bits — bitwise AND lets you 'read' any bit by masking everything else to zero, OR lets you 'write' a bit to 1 without touching its neighbors, XOR lets you 'flip' a bit, and shifts let you move your window of interest left or right. Once you see a number not as a single value but as a tiny register of individually addressable flags, every bit manipulation trick becomes just array indexing with masks.
Binary Endianness: Big-Endian vs. Little-Endian in Multi-Byte Systems
mediumEndianness is simply the answer to the question: when you have a number too large to fit in one byte and you need to lay its bytes out in memory (a sequence of addresses), do you store the most-significant byte first (big-endian, like how we write numbers left-to-right) or the least-significant byte first (little-endian, like reading a number starting from the ones digit)? The concept is trivial in isolation, but it becomes a critical source of bugs the moment two systems that disagree on the convention try to exchange data.
Binary Signed Magnitude vs. Two's Complement: Practical Trade-offs
mediumSigned magnitude is how humans naturally think about negative numbers — slap a sign bit in front and keep the magnitude unchanged — but it creates a hardware nightmare because addition and subtraction become different operations depending on the sign bits, and you get two representations of zero. Two's complement is the encoding trick that lets the same adder circuit handle both positive and negative numbers uniformly, at the cost of a slightly unintuitive bit pattern for negatives; this single design choice eliminates an entire class of special-case logic in every ALU on the planet.
Binary Gray Code: Minimizing Transition Errors in Digital Systems
mediumIn standard binary counting, multiple bits can change at once between consecutive values (e.g., 011 → 100 flips all three bits), and if those bits don't switch at exactly the same instant—which they never do in real hardware—you get a brief, spurious intermediate value that can cause errors. Gray code is a reordering of binary values so that every consecutive pair differs in exactly one bit, eliminating that entire class of glitch by construction.
Binary Hamming Codes: Single-Bit Error Detection and Correction
mediumHamming codes work by strategically placing parity bits at power-of-two positions (1, 2, 4, 8, …) so that each parity bit 'watches' a specific overlapping subset of data bits. When a single bit flips during transmission, the pattern of which parity checks fail (the syndrome) directly encodes the binary position of the corrupted bit — so you don't just know *that* an error happened, you know *exactly where*, and you can flip it back.
Two's Complement Overflow Detection in Arithmetic Operations
mediumIn two's complement, overflow happens when adding two numbers of the same sign produces a result of the opposite sign — because the fixed bit-width ran out of room to represent the true answer. The key detection trick: if the carry into the most-significant bit differs from the carry out of it, the result has silently wrapped around and is wrong. That single XOR of two carry bits is the entire overflow flag.