%3C/text%3E%3C/svg%3E">
💻
💻 Tech & IT

Bit Count (popcount), Leading & Trailing Zeros

Results update as you type.

Your numbers

Understanding the bit count calculator — concept and principle

Population count (popcount) is the number of 1 bits in a binary number; leading and trailing zeros count the 0 bits before the highest and after the lowest set bit. These quantities appear in bitboard chess engines, Hamming distance, error-correcting codes, memory allocators, hash tables sized to powers of two and in choosing how many bits a field needs. Enter an unsigned 32-bit number (0 to 4,294,967,295) to get all of them.

Counting bits in 4040 = 101000: two set bits, 26 leading zeros in 32 bits, 3 trailing zeros, 6 bits needed; 40 AND 39 = 32 so it is not a power of two.40 inside a 32-bit box26 leading zeros6 bits used40 = 32 + 8 = 10100032116081402010popcount 2 (even)3 trailing zeros40 AND 39 = 32 ≠ 0→ not a power of two
Only the last 6 of 32 bits are used; the two 1s give popcount 2 and the three 0s at the end give 3 trailing zeros.

How it is calculated

The principle

In two's complement, −x equals NOT x + 1. Adding 1 to NOT x turns its trailing 1s back to 0s and stops at the first 0, which is exactly where x has its lowest 1, so x AND −x isolates that bit. Subtracting 1 does the mirror image: it clears the lowest 1 and sets the zeros below it, so x AND (x − 1) removes the lowest set bit; it is zero only when x had a single 1 bit, which is the definition of a power of two. Leading zeros measure the position of the highest bit, which is why they give the bit length.

Worked example

  1. Input: 40 = 32 + 8 = 101000 in binary.
  2. Popcount: two 1 bits, so 2; parity is even.
  3. The number uses 6 bits, so 6 bits are needed and leading zeros are 32 − 6 = 26.
  4. 40 AND −40 = 8 = 2³, so trailing zeros are 3.
  5. 40 AND 39 = 101000 AND 100111 = 100000 = 32, not zero, so 40 is not a power of two.

How to read the result

Bits needed tells you the smallest field that can hold the value: 40 fits in 6 bits, since 6 bits cover 0–63. Trailing zeros tell you the largest power of two dividing the number (2³ = 8 divides 40). The popcount of A XOR B is the Hamming distance between two bit patterns.

Common mistakes

Keep in mind: Values are converted to unsigned 32-bit integers. Numbers above 4,294,967,295, negative inputs and decimals are wrapped or truncated, so use a 64-bit or arbitrary-precision tool for larger values.

Frequently asked questions

Is there a CPU instruction for popcount?

Many processors have one (for example POPCNT on x86-64), and compilers expose it as built-ins such as __builtin_popcount.

How do I round up to the next power of two?

For x > 1 it is 2 to the power of (32 − clz32(x − 1)).

What does trailing zeros tell me about divisibility?

A number with k trailing zeros is divisible by 2ᵏ but not by 2ᵏ⁺¹.

Sources: Henry S. Warren Jr., Hacker's Delight, 2nd ed., Addison-Wesley, 2012, Chapter 5 (Counting bits) · Ecma International, ECMA-262 ECMAScript Language Specification (Math.clz32)

Checked 8 Nov 2026. Standard formulas and RFC/IEEE definitions, computed locally in your browser — nothing you type is sent anywhere.

💻 All tech & IT tools →🌍 Calculators by country →