Hamming Weight Calculator (Count Set Bits) – Free

Paste a binary word and this Hamming weight calculator counts how many of its bits are 1. The word 01001101 has a Hamming weight of 4.

How to use the Hamming weight Calculator tool

  1. Type or paste the word whose ones you want counted into the Binary digits box, such as 01001101.
  2. Select Convert to count the set bits.
  3. Read the Hamming weight, a whole number, in the Result box.
  4. Select Copy to copy the count, or Example to load the sample byte.

What Hamming weight is

The Hamming weight of a word is the number of symbols in it that are not zero. For a binary word that is simply the number of 1 bits. The same quantity is called population count, popcount or sideways sum. It equals the Hamming distance between the word and an all-zeros word of the same length.

The tool removes spaces and an optional 0b prefix, then counts the remaining 1 characters. Zeros, including leading zeros, contribute nothing, so 1101 and 00001101 both have weight 3. Counting happens in the browser with no upload.

Hamming weight examples

The last line is the subnet mask 255.255.255.0 in binary. Its weight, 24, is the prefix length in CIDR notation, which is why a popcount turns a netmask into the /24 form.

01001101                          ->  4
11111111                          ->  8
0000                              ->  0
11111111111111111111111100000000  ->  24

How processors and languages count bits

Most CPUs do this in one instruction: POPCNT on x86 and CNT on Arm NEON. Compilers and standard libraries expose it under various names.

Without hardware support, a classic loop clears the lowest set bit with n = n AND (n - 1) and counts the iterations, so it runs once per 1 bit. Many textbooks credit that trick to Brian Kernighan.

  • C and C++: __builtin_popcount in GCC and Clang, std::popcount in C++20.
  • Java: Integer.bitCount and Long.bitCount.
  • Python: int.bit_count() from version 3.10.
  • Go: bits.OnesCount in the math/bits package.
  • Rust: the count_ones method on integer types.

What the count is used for

A bit count answers practical questions: how many flags are enabled in a mask, how many blocks are marked used in a bitmap, how many pieces stand on a chess bitboard, or how many items are in a set stored as bits. Paired with XOR it yields Hamming distance, which drives error-correcting codes and similarity search over binary fingerprints. Parity is the weight modulo 2.

The Population count Calculator on this site returns the same number. Use whichever name matches the term in your documentation.

Frequently asked questions

What is the Hamming weight of 01001101?

It is 4. The ones sit at bit positions 6, 3, 2 and 0, counted from the right. The leading zero does not affect the count.

Is Hamming weight the same as popcount?

Yes. Hamming weight is the term from coding theory and popcount, or population count, is the term used for CPU instructions and library functions. Both mean the number of 1 bits in a word.

How do I find the Hamming weight of a decimal or hex number?

Convert it to binary first, for example with the Decimal to Binary or Hexadecimal to Binary tool, then paste the bits here. Hex FF becomes 11111111, which has weight 8.

What is the largest input this calculator accepts?

Up to 65,536 binary digits, not counting spaces or a leading 0b. An empty box or any character other than 0 and 1 returns an error message.

How is Hamming weight related to parity?

Parity is whether the weight is even or odd. A weight of 4 means even parity, so an even parity bit for that word would be 0. The Parity Calculator reports both the count and the parity.

How do I count the zero bits instead?

Subtract the Hamming weight from the word length. For 01001101 that is 8 minus 4, or 4 zeros. This is useful when the clear bits are the resource, such as free slots in an allocation bitmap.

Related tools