Visual walkthrough — Fenwick tree (Binary Indexed Tree) — prefix sums, O(log n) update and query
Step 1 — The problem, drawn as a row of boxes
WHAT. We have a row of numbered boxes. Box number holds a value . We number them starting at (not — that choice matters later, and we will see exactly why).
WHY. Before inventing a clever structure, look at the enemy. A prefix sum up to box means "add everything from box to box ." Written out:
Every symbol here: are the numbers inside the boxes; the signs are ordinary addition; is the single total we want. Doing this the naive way visits every box — that is steps, which for a big row is slow.
PICTURE. Below, the top row is the raw data. The bracket shows the six boxes a naive would have to touch one by one.

Step 2 — The lowbit ruler: how binary tells us a block size
WHAT. Write each box number in binary (base-2: each digit is a power of two, ). The lowest set bit of a number is the rightmost in that binary writing, read as its own power of two. We call it .
WHY this tool and not another. We need a rule that assigns each box a block length so that (a) big blocks and small blocks nest neatly, and (b) the rule is computable in one machine instruction. The rightmost binary does both: it is the largest power of two that divides , and hardware computes it with . We reach for binary precisely because "powers of two" and "nesting" are the same idea in binary.
Term by term: is the box number; is its negative written in two's complement (flip all bits, add ); is bitwise-AND (keep a bit only where both have a ). The flip-and-add-one leaves every bit above the rightmost disagreeing, so only that single rightmost survives the AND.
PICTURE. The board shows next to ; the AND lights up exactly the bit.

See Binary representation of integers if the base-2 writing itself feels new.
Step 3 — Each cell owns a block ending at itself
WHAT. We keep a second array, , the same length as the data. Cell does not store alone — it stores the sum of a block of consecutive values that ends at and has length :
Term by term: the sum runs over ; it ends at (right edge of the block); it starts at (walk left by boxes, then to land on the first box inside the block). So the block has exactly boxes.
WHY. If short indices own short blocks and indices divisible by big powers of two own big blocks, then a few of these blocks tile any prefix — that is the payoff we cash in Step 4.
PICTURE. Each cell drawn as a horizontal bar showing which raw boxes it covers. Notice how bars of length stack like a staircase.

| binary | lowbit | covers | |
|---|---|---|---|
| 4 | 4 | ||
| 6 | 2 | ||
| 7 | 1 | ||
| 8 | 8 |
Step 4 — Query: cover a prefix with a few bars (walk DOWN)
WHAT. To get , add , then jump to the box just left of that block by subtracting the lowbit, and repeat until you fall off the left edge (reach ):
Term by term: contributes one bar; is the box where the next bar's right edge sits (we chopped off exactly the block we just used); the loop stops at because there is nothing before box .
WHY it is fast. Each subtraction erases the rightmost from 's binary form. A number with at most bits has at most ones, so at most bars are added. This is the whole speedup, and it comes for free from the lowbit rule of Step 2.
PICTURE. For : start at (bar ), drop to (bar ), drop to stop. Two bars tile perfectly — no overlap, no gap.

Compare this to a plain Prefix sum array: that array answers the query in one step but pays per update — the trade we are escaping.
Step 5 — Update: fix every bar covering a box (walk UP)
WHAT. To add to , we must repair every bar whose block contains box . Start at , then climb by adding the lowbit, until we pass the end :
Term by term: is the smallest bar containing box (its block ends at ); is the index of the next larger bar that still swallows box — its left edge is the same or further left, and by the nesting of Step 3 its index is exactly plus its own lowbit; the loop stops once exceeds the array size .
WHY the opposite direction. Query chops the rightmost (goes toward ); update carries into the next bit (goes toward ). They walk the same lowbit ladder in mirror directions — that symmetry is the heart of the structure.
PICTURE. Updating box : the arrow climbs stop. Highlighted bars are exactly the ones covering box .

Step 6 — Range sums by subtracting two prefixes
WHAT. A sum over is two prefix queries subtracted:
Term by term: is everything up to ; is everything strictly before ; subtracting cancels boxes and leaves exactly .
WHY , not . Box itself belongs to the range, so we must remove only what comes before it. Using would wrongly delete .
PICTURE. Two brackets — the big one minus the small one — leaving the middle strip shaded.

Step 7 — The degenerate case: why box 0 breaks everything
WHAT. Look at . Zero in binary is all s — it has no rightmost . So .
WHY it is fatal. In the update loop, — the index never moves, an infinite loop. In the query loop, we stop the moment , so an index of contributes nothing and can never start a walk. Index has no block. That is the real reason a Fenwick tree lives on indices ; if your data is -indexed, shift it by one.
PICTURE. The board shows the number line: every positive index has a rightmost (an arrow of length lowbit), but is a dead stop with a zero-length arrow.

The one-picture summary
Everything above compressed: the raw boxes on top; the staircase of bars (lengths ) below; a blue query path chopping down from to ; a pink update path climbing up from past . Query and update are the same ladder walked in opposite directions.

Recall Feynman: the whole walkthrough in plain words
Picture a shelf of numbered candy jars. Instead of counting jars one by one to know "how much candy in the first six jars," some jars keep a pre-bundled subtotal of a chunk of jars ending at them. How big is the chunk? Look at the jar's number in binary and find the rightmost 1 — that power of two is the chunk length (jar , rightmost is , so it bundles jars: and ). To total the first six, you grab jar 's bundle (jars ), then step back to jar whose bundle is jars – — two grabs, done, no gaps. To change one jar, you fix only the bundles that contain it, found by repeatedly adding the rightmost-1 chunk (jar ). Grabbing bundles walks down toward zero; fixing bundles walks up toward the end — same ladder, opposite ways. And jar ? It has no rightmost , no chunk, so it never plays — which is exactly why we number from .
Recall Self-test
For , which cells add up? ::: (bar ) then : (bar ) then : (bar ) then stop. Cells . Which cells does updating box touch (n=8)? ::: . Cells . Why does query terminate in steps? ::: Each subtraction deletes one binary from the index; a number below has ones.
Connections
- Parent: Fenwick tree (Binary Indexed Tree) — prefix sums, O(log n) update and query
- Contrast the trade-offs: Prefix sum array, Segment tree
- Bit machinery behind lowbit: Two's complement representation, Binary representation of integers
- Applications built on this: Inversion counting, Order statistics, Range update with difference array