How to use the Population count Calculator tool
- Paste a binary word into the Binary digits box. Groups separated by spaces, such as 1011 0110, are accepted.
- Select Convert, or Example to count the bits of the sample 01001101.
- Read the number of one bits in the Result box.
- Select Copy to copy the count, or Clear to enter a new word.
What population count measures
The population count of a word is how many of its bits are 1. Zeros are ignored, so the width of the word and any leading zeros have no effect on the answer. For 01001101 the count is 4.
The calculator works on the text you enter: it removes whitespace and an optional 0b prefix, checks that only 0 and 1 remain, and counts the 1 characters. Words up to 65,536 bits are supported, far wider than any machine register.
Worked examples, including a subnet mask
A handy real-world use is turning an IPv4 subnet mask into its prefix length. The mask 255.255.255.0 in binary is three bytes of ones followed by a byte of zeros. Its population count is 24, which is the /24 in CIDR notation.
01001101 → 4
1011 0110 → 5
11111111 11111111 11111111 00000000 → 24
0000 → 0How popcount is computed in code
The obvious loop tests one bit at a time. A well-known shortcut, commonly credited to Brian Kernighan, repeats x = x AND (x - 1), which clears the lowest set bit on each pass, so the loop runs once per 1 bit and not once per position.
Most platforms have a faster path. x86 processors provide a POPCNT instruction, and languages wrap it: __builtin_popcount in GCC and Clang, std::popcount in C++20, Integer.bitCount in Java, int.bit_count() in Python 3.10 and later, bits.OnesCount in Go and count_ones in Rust. Use this page to produce expected values for unit tests of those calls.
Where developers use population count
Counting set bits appears wherever data is packed into bit vectors, from databases to game engines.
- Hamming distance: the popcount of a XOR b is the number of positions where two words differ.
- Bitboards in chess engines, to count pieces or attacked squares.
- Bitmap indexes and bitsets, to count matching rows or set members.
- Hash array mapped tries, where the popcount of a masked bitmap gives the slot of a child node.
- Feature or permission flags, to count how many options are enabled.
Frequently asked questions
What is the population count of a binary number?
It is the number of 1 bits in the number's binary form. The word 10110001 has a population count of 4, and a word of all zeros has a population count of 0.
Is popcount the same as Hamming weight?
Yes. Hamming weight is the term from coding theory and population count is the term from computer architecture. For binary words they are the same number, and the Hamming weight Calculator on this site returns the same result.
Do leading zeros change the population count?
No. Only 1 bits are counted, so 1101, 00001101 and a 64-bit version of the same value all return 3. You can paste a word at any width.
Can I count bits in a hex or decimal number?
Not directly. The field accepts binary digits only. Convert the number with the Hexadecimal to Binary or Decimal to Binary tool first, then paste the bits here.
How long can the input be?
From 1 to 65,536 binary digits, not counting spaces, line breaks or a 0b prefix. Longer input, an empty box or other characters produce the message Enter between 1 and 65,536 binary digits.
How does this differ from the Parity Calculator?
Population count gives the full number of 1 bits. Parity keeps only whether that number is even or odd. Use popcount when the size of the count matters and parity when you are checking a parity-protected frame.