Binary Zeros Counter
Count zeros in binary representation of any number. Analyze leading, trailing, consecutive zero runs, and zero density from decimal, hex, or binary input.
About
Every integer maps to a unique binary string of 0s and 1s. The distribution of zeros within that string is not trivial. Leading zeros depend on word size. Trailing zeros equal the largest power of 2 that divides n. Consecutive zero runs affect Hamming weight calculations and carry-chain length in adder circuits. Miscounting zeros leads to incorrect bitmask construction, broken permission flags, and off-by-one bugs in network subnet calculations. This tool parses decimal, hexadecimal, octal, or raw binary input and returns a complete zero-distribution profile: total zeros, leading zeros, trailing zeros, longest zero run, zero density ฯ0, and a positional bit map. It assumes unsigned magnitude representation. Negative values are handled via two's complement at a configurable word size.
Formulas
Given an input value, convert it to its unsigned binary string B of length n. The zero density is defined as:
where Z = total count of 0 bits in B, and n = bit length of B.
Trailing zeros equal the 2-adic valuation ฮฝ2(n), i.e., the exponent of the highest power of 2 dividing n:
Leading zeros depend on the chosen word size W (e.g., 8, 16, 32, 64):
where L = leading zero count, W = word size in bits, n = significant bit length. The longest zero run is found by iterating through B and tracking the maximum consecutive 0 subsequence length. Hamming weight (popcount) is H = n โ Z, the count of 1 bits.
Reference Data
| Decimal | Binary | Bit Length | Total Zeros | Leading Zeros (8-bit) | Trailing Zeros | Longest Zero Run | Zero Density |
|---|---|---|---|---|---|---|---|
| 0 | 0 | 1 | 1 | 7 | 1 | 1 | 100% |
| 1 | 1 | 1 | 0 | 7 | 0 | 0 | 0% |
| 2 | 10 | 2 | 1 | 6 | 1 | 1 | 50% |
| 7 | 111 | 3 | 0 | 5 | 0 | 0 | 0% |
| 8 | 1000 | 4 | 3 | 4 | 3 | 3 | 75% |
| 10 | 1010 | 4 | 2 | 4 | 1 | 1 | 50% |
| 15 | 1111 | 4 | 0 | 4 | 0 | 0 | 0% |
| 16 | 10000 | 5 | 4 | 3 | 4 | 4 | 80% |
| 42 | 101010 | 6 | 3 | 2 | 1 | 1 | 50% |
| 100 | 1100100 | 7 | 4 | 1 | 2 | 2 | 57.1% |
| 127 | 1111111 | 7 | 0 | 1 | 0 | 0 | 0% |
| 128 | 10000000 | 8 | 7 | 0 | 7 | 7 | 87.5% |
| 255 | 11111111 | 8 | 0 | 0 | 0 | 0 | 0% |
| 256 | 100000000 | 9 | 8 | 0 | 8 | 8 | 88.9% |
| 1000 | 1111101000 | 10 | 4 | 0 | 3 | 3 | 40% |
| 1023 | 1111111111 | 10 | 0 | 0 | 0 | 0 | 0% |
| 1024 | 10000000000 | 11 | 10 | 0 | 10 | 10 | 90.9% |
| 2048 | 100000000000 | 12 | 11 | 0 | 11 | 11 | 91.7% |
| 4096 | 1000000000000 | 13 | 12 | 0 | 12 | 12 | 92.3% |
| 65535 | 1111111111111111 | 16 | 0 | 0 | 0 | 0 | 0% |