Bits of an integer
An integer is a row of bits. Shifting moves the bits one place, AND with 1 reads the lowest bit, and AND with a mask keeps only chosen bits. The bit tricks here combine these to count or clear bits without looping over all 32 positions.
Memory layout
A single machine word (32 bits here); every operation below works on registers, not memory.
| Operation | Cost |
|---|---|
| test one bit | O(1) |
| count all bits | O(32), O(bits set) or O(1) depending on the method |