On this page
A plain array changes a value in one step but needs up to n additions for a running total. A prefix-sum array gives any total in one step but must rewrite up to n entries after each change. A Fenwick tree does both in O(log n) steps; our guide to Big O notation and growth rates explains the notation.
What is a Fenwick tree (binary indexed tree)?
A Fenwick tree is a data structure that stores an array of values and can efficiently compute prefix sums of those values and update them, in Wikipedia's definition. Peter Fenwick described it in 1994 in Software: Practice and Experience as a way to maintain the cumulative frequencies needed for dynamic arithmetic data compression. The same Wikipedia article notes that Boris Ryabko proposed the structure in 1989. If you have read how arithmetic coding compresses a whole message into one number, those cumulative frequencies are the intervals it slices up.
There are no pointers: it is one array T[1..n]. Like the binary heap behind a priority queue, the tree is implicit in the indices. Cell i stores the sum of a block of values ending at position i, and the block's length is the lowest set bit of i.
What does i & (−i) do?
The lowest set bit of i is the value of its rightmost 1 in binary: for 6 = 110 it is 2, for 8 = 1000 it is 8. In two's complement arithmetic it is computed as i & (−i), as the Wikipedia article states. In 8 bits, 6 = 00000110 and −6 = 11111010; ANDing them leaves 00000010, which is 2.
So T[i] covers positions i − lsb(i) + 1 to i. These are the responsibility ranges in the diagram above:
Odd i (1, 3, 5, 7): the lowest set bit is 1, so T[i] holds one value.
i = 2 and i = 6: the lowest set bit is 2, so two values.
i = 4: positions 1 to 4.
i = 8: the whole array.
How do query and update walk the tree?
Take A = [3, 2, 5, 1, 4, 6, 2, 7] at positions 1 to 8. The tree is:
T[1] = 3, T[2] = 3 + 2 = 5, T[3] = 5, T[4] = 3 + 2 + 5 + 1 = 11
T[5] = 4, T[6] = 4 + 6 = 10, T[7] = 2, T[8] = 11 + 4 + 6 + 2 + 7 = 30
To find the sum of the first 7 values, start at i = 7. Add T[i], then strip the lowest set bit: i becomes i − (i & −i).
i = 7 (111): add T[7] = 2, total 2.
i = 6 (110): add T[6] = 10, total 12.
i = 4 (100): add T[4] = 11, total 23.
i = 0: stop.
Check: 3 + 2 + 5 + 1 + 4 + 6 + 2 = 23. Each step clears one 1-bit of i, so a query visits at most as many cells as i has 1-bits, never more than about log₂ n + 1.
A range sum is two queries. The sum of positions 3 to 6 is prefix(6) − prefix(2) = (10 + 11) − 5 = 16, and indeed 5 + 1 + 4 + 6 = 16.
To add 3 to A[3], walk upward instead, adding the lowest set bit: i becomes i + (i & −i).
i = 3 (011): T[3] becomes 8.
i = 4 (100): T[4] becomes 14.
i = 8 (1000): T[8] becomes 33.
i = 16: past n, stop.
Those are exactly the three cells whose ranges contain position 3. Query 7 again: 2 + 10 + 14 = 26, which is 23 + 3.
Fenwick tree vs segment tree: which should you use?
The cp-algorithms segment tree guide says a segment tree processes both queries in O(log n) time, and the cp-algorithms Fenwick tree guide gives O(log N) for each update and range query. The differences:
Memory. A Fenwick tree needs O(N) memory, the same as the array itself (cp-algorithms). The standard segment tree is given an array of 4n vertices for an array of size n, although only 2n − 1 of them are needed (cp-algorithms).
Code. Each Fenwick operation is a short loop over the bits of the index.
What it can answer. A Fenwick range sum works because subtraction undoes addition: prefix(r) − prefix(l − 1). A minimum cannot be undone that way, so a plain Fenwick tree cannot give the minimum of an arbitrary range. A segment tree can find the minimum or maximum in a range, and with lazy propagation it can change a whole segment at once (cp-algorithms).
A working rule: sums or counts with single-value updates, use a Fenwick tree; minimum, maximum or range assignment, use a segment tree.
How should you practise it?
Pick 8 values of your own and fill T[1..8] by hand from the ranges.
Write each index in binary; trace one query and one update.
Code both loops and check them against brute-force sums on random arrays.
If binary representation feels shaky, the Fermi Concept Library covers concepts in mathematics and computing, each with its own lesson, so you can revisit it before tracing again.



