Bit Population Count (popcount)

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

Count the set bits in any integer — the Hamming weight or popcount. Enter a value in decimal, hexadecimal, or binary and see how many 1 bits it contains, the zero count, and its binary representation. Handles arbitrarily large integers in your browser. It runs free in your browser on Gera Tools, with nothing uploaded.

Last updated Source: Gera Tools

What is a population count?

The population count, also called the Hamming weight, is the number of bits set to 1 in a number's binary representation. For example, the byte 10110110 has a population count of 5.

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 individually:

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

Subtracting 1 from x flips the lowest set bit to 0 and turns every zero below it into 1. ANDing that result with the original x clears the lowest set bit (and everything below) in one step. The loop therefore runs exactly once per set bit, not once per bit position — making it fast when set bits are sparse. The zero count shown is the number of zero bits within the minimal width needed to write the number, since leading zeros are theoretically infinite.

Worked example

The decimal value 182 is 10110110 in binary:

Bit 7Bit 6Bit 5Bit 4Bit 3Bit 2Bit 1Bit 0
10110110

Five positions are 1, so the population count is 5. The remaining three positions are 0, giving three zero bits across the eight-bit width.

Tracing Kernighan’s loop:

Stepx (binary)x & (x-1)Bits cleared
11011011010110100bit 1
21011010010110000bit 2
31011000010100000bit 4
41010000010000000bit 5
51000000000000000bit 7

Five iterations, five set bits — the loop halts.

Where popcount is used

  • Bitboard engines (chess, draughts): counting pieces on a 64-bit board in a single instruction.
  • Error-correcting codes: Hamming distance between two codewords equals the popcount of their XOR — if two words differ in three bit positions, their XOR has a population count of 3.
  • Bloom filters: monitoring how many bits are set to estimate the filter’s saturation before false-positive rates climb.
  • Similarity hashing: comparing SimHash fingerprints of documents by their Hamming distance to cluster near-duplicates.
  • Cryptography: certain side-channel attacks exploit differences in power consumption that correlate with popcount, so constant-time implementations avoid data-dependent loops.

Modern CPUs expose this natively: the POPCNT instruction on x86/x64 and the CNT instruction on ARM can count set bits in a 64-bit register in a single clock cycle. This calculator uses JavaScript BigInt, so there is no 32- or 64-bit limit — you can analyse numbers hundreds of digits long.