Bit Population Count (popcount)

Count the number of set (1) bits in any integer

Ad placeholder (leaderboard)

Population count, also known as the Hamming weight or simply popcount, answers a deceptively useful question: how many bits in this number are set to 1? This tool computes it for any non-negative integer entered in decimal, hex, or binary, and shows the binary form so you can verify the count by eye.

How it works

The tool counts set bits using Kernighan’s bit-clearing trick rather than checking every bit position:

count = 0
while x != 0:
    x = x & (x - 1)   // clears the lowest set bit
    count += 1

Subtracting 1 flips the lowest set bit to 0 and turns the zeros below it into ones; ANDing with the original then clears that bit and everything below. Because the loop runs exactly once per set bit, it is faster than scanning all bits when set bits are sparse. The zero count is the total bit width of the number minus the population count.

Example and notes

The decimal value 182 is 10110110 in binary, which has five 1 bits, so its population count is 5 and it has three zero bits across its eight-bit width. Real CPUs expose this as the POPCNT (x86) or CNT (ARM) instruction, and it underpins bitboard chess engines, Bloom-filter saturation checks, and Hamming distance between hashes. This calculator uses BigInt, so there is no upper limit on the size of the integer you can analyse.

Ad placeholder (rectangle)