Foundations — Bit manipulation — XOR tricks, LSB, counting set bits
This page builds every symbol the parent note leans on, from absolute zero. If the parent note said "just XOR the array" or "x & -x", we slow down and earn each piece first. Read top to bottom; nothing appears before it is defined and drawn.
0. What is a "bit"? (the switch)
Picture a single light switch on a wall. It has exactly two states. That is a bit. A computer number is a whole row of these switches sitting side by side.

Why does the topic need this? Because every trick later ("toggle a switch", "find the lowest ON switch") is a statement about this row. If you cannot see the row, the tricks are just cryptic symbols.
1. Place value — why each switch is worth a doubling
Each switch is not worth the same. Reading the row right to left, the switches are worth i.e. each switch is worth twice the one to its right. These are the powers of two.
The value stored by a whole row is: add up the doubling-values of only the switches that are ON.

Related vault reading: Logarithms and Powers of Two — the exponent is literally the "log base 2" of the place value.
2. The summation symbol (writing "add them all up")
The parent note writes the number as That looks scary. It is just section 1 in shorthand. Let's earn every symbol.
So the formula reads, in English: "add up each switch's contribution, from the rightmost () to the leftmost ()." Exactly what we did to get 13.
3. Position, and the LSB / MSB names
The parent note's LSB tricks are all about position and about the lowest ON switch — so this naming has to be rock solid before section 4.
4. The core operators — one picture each
These operators act on two rows, column by column: they line the rows up and decide each output switch from the pair directly above it.

Recall Say each operator's rule in five words
AND ::: on only if both on OR ::: on if either is on XOR ::: on only if they differ NOT ::: flip every single switch
5. The symbol — XOR written in maths
Programmers write XOR as ^. Mathematicians write the same thing as (a plus inside a circle). The parent note uses in its formulas.
Why does the topic hinge on this one operator? Because XOR-ing a value with itself gives all-zeros (: every column has two equal switches, so none "differ"). That single fact makes duplicates self-destruct — the parent note's "cancellation engine". We earn the deep version of that in Deep Dive D2; here we just make sure you can read the symbol.
6. Shifting — << and >> (slide the whole row)
7. Negatives and two's complement — where -x comes from
The parent note's star trick is x & -x, and it insists two's complement is "the secret". So we must define what a negative row even means.
Why this bizarre recipe? Because it makes ordinary addition "just work" — rolls over to all-zeros, exactly as should. Full justification lives in Two's Complement Representation; for our tricks you only need the recipe and one consequence, drawn below.

The picture shows the key consequence used by x & -x: below the lowest ON switch, looks identical to (both all-OFF), and at that lowest ON switch both are ON, while everything above disagrees. That is precisely why AND-ing with leaves only the lowest ON switch — but we build that derivation carefully in Deep Dive D3. Here you just need to trust the recipe and see the alignment.
8. Big-O — the promise these tricks make
The parent note keeps saying "", "", "". These describe how the work grows as the input grows.
Why the topic cares: bit tricks often replace an -memory hash-set approach with an -memory one — see Hash Sets vs O(1)-space tricks. That trade is the whole selling point.
Prerequisite map
Equipment checklist
Reveal each line and check you can answer without hesitation. If any stalls you, reread that section before tackling the XOR / LSB / popcount deep dives.
What is a bit and its two states?
What is the place value of switch at position ?
Evaluate the row 1011.
Read in plain English.
Which switch is the LSB and what is it worth?
XOR () output rule?
What is ?
What does x << k equal arithmetically?
What does x >> k equal for non-negative ?
Give the two's complement recipe for .
~x) then add 1.Why isn't ~x alone equal to ?
~x equals ; you must add 1.What does space mean?
Connections
- Bit manipulation — parent topic — the tricks these foundations unlock.
- Two's Complement Representation — full story behind the
-xrecipe in section 7. - Logarithms and Powers of Two — the exponent as of the place value.
- Hash Sets vs O(1)-space tricks — why the Big-O of section 8 matters.
- Bitmask Dynamic Programming — treating a whole switch-row as a set.
- Greedy and Divide-and-Conquer (Algorithm Paradigms)