AlgoMaster Logo

Number of 1 Bits

easyFrequency6 min readUpdated June 23, 2026

Understanding the Problem

We're given a positive integer and need to count how many bits in its binary form are 1. For instance, the number 11 in binary is 1011, so the answer is 3.

This is a bit manipulation problem. The direct approach checks each bit one by one. A faster variant uses n & (n - 1) to skip over the zero bits and touch only the set bits. And if the function gets called millions of times, a precomputed lookup table answers each query with a handful of array accesses.

Key Constraints:

  • 1 <= n <= 2^31 - 1 means the input is a positive 32-bit integer, so there are at most 31 bits to inspect. Every approach below runs in O(1) time on this fixed width. The difference between them is the constant factor: a bit-by-bit scan does 31 iterations, Brian Kernighan's does one iteration per set bit, and a lookup table does four array accesses with no loop.
  • Because n is guaranteed positive, the sign bit is never set, so plain right shifts and signed comparisons behave the same as their unsigned counterparts.

Approach 1: Check Each Bit

Intuition

Count set bits by looking at each bit position one at a time. The expression n & 1 is 1 if the least significant bit is set and 0 otherwise. After inspecting that bit, right-shift n to move the next bit into the least significant position, and repeat until n reaches 0.

Since n has at most 31 bits, this loop runs at most 31 times.

Algorithm

  1. Initialize a counter count to 0.
  2. While n is greater than 0:
    • If n & 1 equals 1, increment count.
    • Right-shift n by 1.
  3. Return count.

Example Walkthrough

1n = 11 (binary: 1011). Check bit by bit from right to left.
0
1
1
0
2
1
3
1
1/6

Code

This loop runs once per bit position, so it touches every zero bit even when only one bit is set. The next approach iterates once per set bit instead, skipping the zeros entirely.

Approach 2: Brian Kernighan's Algorithm

Intuition

The expression n & (n - 1) clears the lowest set bit of n. Subtracting 1 flips that lowest set bit to 0 and turns every bit below it into a 1, while leaving every bit above it unchanged. ANDing n with that result keeps the high bits as they were, zeros out the lowest set bit, and zeros out everything below it (those positions are 0 in n). The net effect is that exactly one set bit, the lowest, is removed.

So instead of checking every bit position, keep clearing the lowest set bit until n becomes 0. The number of iterations equals the number of set bits. A number with 2 set bits out of 31 loops twice, not 31 times.

Algorithm

  1. Initialize a counter count to 0.
  2. While n is greater than 0:
    • Clear the lowest set bit: n = n & (n - 1).
    • Increment count.
  3. Return count.

Example Walkthrough

1n = 11 (binary: 1011). Clear lowest set bit each iteration.
0
1
1
0
2
1
3
1
1/5

Code

Brian Kernighan's algorithm still runs a loop on every call. When hammingWeight is invoked many times, the follow-up asks how to do better. The next approach precomputes the bit count for every possible byte and replaces the loop with a fixed number of table lookups.

Approach 3: Lookup Table

Intuition

This approach answers the follow-up. Precompute the number of set bits for every byte value (0 to 255) and store them in a 256-entry table. A 32-bit integer is four bytes, so counting its set bits becomes four table lookups added together, with no per-bit loop.

The cost is 256 entries of precomputed data, a fixed amount of memory that is reused across every call. In exchange, each query after the table is built costs four array accesses and three additions.

Algorithm

  1. Build a table of size 256 where table[i] stores the number of set bits in i.
  2. For the input n, extract each of its 4 bytes using bit masking and shifting.
  3. Look up each byte in the table and sum the results.
  4. Return the sum.

Example Walkthrough

1n = 11. Split into 4 bytes: [0, 0, 0, 11]
0
0
1
0
2
0
3
11
1/6

Code