Hamming Weight Calculator

Enter a number and select its base.

How it is calculated

You type a non-negative whole number and choose its base: decimal, hexadecimal or binary. The tool converts the value to an exact binary string and counts the 1-bits: weight = number of 1s in the binary form. It also shows the binary string and its length in bits, without leading zeros (zero itself is one bit long, 0). A leading 0x or 0b is accepted in hex and binary mode only. Anything else, such as a minus sign, a decimal point, a comma, a space or a digit not valid in the chosen base, gives no result. The value must fit in 64 bits, up to 18,446,744,073,709,551,615 (FFFFFFFFFFFFFFFF in hex); a longer value shows no result. Because the conversion works on digit strings, large values are exact.

Worked example

Take DEADBEEF in hexadecimal. Each hex digit is four bits: D = 1101 (three 1s), E = 1110 (three), A = 1010 (two), D (three), B = 1011 (three), E (three), E (three), F = 1111 (four). Sum: 3 + 3 + 2 + 3 + 3 + 3 + 3 + 4 = 24. The binary form is 11011110101011011011111011101111, which the tool reports as 32 bits long, so 24 of its 32 bits are set and 8 are clear.

Try this example →

Common mistakes

  • Leaving the base on decimal while typing a hex or binary value: 1011 in decimal is the number one thousand eleven (weight 8), while 1011 in binary is eleven (weight 3).
  • Counting leading zeros. They are not shown, are not part of the bit length, and never change the weight.
  • Entering a negative number. The tool does not apply two's complement and gives no result for a minus sign, so work out the bit pattern first and enter it in hex or binary.
  • Typing a number with separators such as 1,024 or 1 024. Commas and spaces are not allowed, only digits.
  • Putting a 0x prefix on a number while the base is set to decimal. The prefix is only accepted in hex mode (and 0b in binary mode), so the entry is rejected.

About this calculator

The Hamming weight of a number — also called its population count or popcount — is simply the count of 1-bits in its binary representation. For example, the decimal number 13 is 1101 in binary, giving it a Hamming weight of 3. This calculator accepts a number in decimal, hexadecimal, or binary and counts its set bits directly.

Popcount is a workhorse operation in computer science and digital electronics: it measures the “distance” between two binary codes in error-detecting and error-correcting codes (the Hamming distance between two values is the popcount of their XOR), it underlies bit-manipulation tricks in cryptography and hashing, and it appears in digital signal processing and chess-engine bitboards, where CPUs even provide a dedicated POPCNT instruction because the operation is so common.

Was this helpful?

You can pick up to 3 reactions.

Comments (1)

  • Editorial team

    This counts the ones in a single value, so to get the Hamming distance between two words you first combine them with XOR yourself. XOR of F0 and CC is 3C in hex, and entering 3C returns a weight of 4, so those two bytes differ in four bit positions. Values up to 64 bits are accepted, and leading zeros do not change the weight. Which word size do you work with, and would wider values help?

Popular calculators

All Calculators