The rank operation counts occurrences of a bit value up to a position:
- = count of 0s in
- = count of 1s in
function rank(bitvector, bit, i)
count := 0
for j from 0 to i - 1
if bitvector[j] = bit then
count := count + 1
return count
Naive: . With preprocessing, you can do using prefix sums.
Note: .