User Rating 0.0 โ˜…โ˜…โ˜…โ˜…โ˜…
Total Usage 0 times
Accepts decimal, binary (0b), hex (0x), octal (0o). Underscores allowed.
Examples:
Enter a number and press Count Ones to see results.
Is this tool helpful?

Your feedback helps us improve.

โ˜… โ˜… โ˜… โ˜… โ˜…

About

The count of 1-bits in a binary representation is formally called the Hamming weight or population count (popcount). It appears in error-correcting codes, cryptographic hash analysis, combinatorial optimization, and low-level CPU instruction design. Getting it wrong in a checksum computation or a bitmask filter means silent data corruption. This tool computes the exact Hamming weight for integers of arbitrary size using Brian Kernighan's algorithm, which runs in O(k) time where k equals the number of set bits. It accepts decimal, binary (0b prefix), hexadecimal (0x), and octal (0o) input. Negative integers are not supported because two's complement width is ambiguous without a fixed register size.

binary ones counter hamming weight popcount bit counter set bits binary analysis

Formulas

The Hamming weight (population count) of a non-negative integer n is the number of 1-bits in its binary expansion.

popcount(n) = L โˆ’ 1โˆ‘i = 0 bi

where bi โˆˆ {0, 1} is the i-th bit and L is the bit length of n.

Brian Kernighan's algorithm exploits the fact that n โˆง (n โˆ’ 1) clears the lowest set bit. Repeat until n = 0 and count iterations.

k = 0
while n โ‰  0:
n โ† n โˆง (n โˆ’ 1)
k โ† k + 1
return k

Bit density is defined as: density = popcount(n)L ร— 100%

where L = โŒŠlog2(n)โŒ‹ + 1 for n > 0, and L = 1 for n = 0.

Reference Data

DecimalBinaryOnes CountBit LengthDensityNotes
00010%Zero case
1111100%Single bit
711133100%All ones (23 โˆ’ 1)
810001425%Power of 2
15111144100%All ones (nibble)
421010103650%Alternating pattern
127111111177100%Max signed 8-bit
128100000001812.5%Power of 2
2551111111188100%Full byte
2561000000001911.1%Power of 2
102311111111111010100%210 โˆ’ 1
1024100000000001119.1%1 KiB boundary
409610000000000001137.7%Page size
6553511111111111111111616100%Full 16-bit word
214748364731 ones3131100%Max signed 32-bit
429496729532 ones3232100%Max unsigned 32-bit
37359285590xDEADBEEF243275%Classic debug marker
34056915820xCAFEBABE223268.8%Java class magic number
488790xBEEF121675%Common test value
170101010104850%0xAA alternating

Frequently Asked Questions

JavaScript's Number type uses IEEE 754 double-precision floats, which can only represent integers exactly up to 253 โˆ’ 1 (Number.MAX_SAFE_INTEGER = 9,007,199,254,740,991). Beyond that threshold, bit-level operations produce incorrect results because the least significant bits are silently truncated. BigInt supports arbitrary-precision integers, so the popcount remains exact for numbers with hundreds or thousands of digits.
A naive approach iterates over every bit position (O(L) where L is the bit length). Kernighan's algorithm runs in O(k) where k is the number of set bits. For sparse numbers like powers of 2, this means a single iteration instead of L. The trick: n & (n โˆ’ 1) always clears exactly the lowest set bit because subtracting 1 flips all bits from the lowest set bit downward.
Bit density (ones / total bits ร— 100%) indicates how "heavy" a binary value is. In networking, high-density subnet masks indicate broader address ranges. In cryptography, hash outputs should have approximately 50% density; significant deviation may signal weakness. In compression, low-density values indicate redundancy. Hardware engineers use density to estimate power consumption because transistor switching correlates with the number of 1-bits.
The Hamming weight of a negative integer depends on the register width. In 8-bit two's complement, โˆ’1 has 8 ones. In 32-bit, it has 32. In 64-bit, 64. Without a fixed width, the count is ambiguous. This tool computes the Hamming weight of the mathematical (non-negative) integer itself. If you need two's complement popcount, convert manually using a specific bit width first.
Yes. The tool accepts four formats: plain decimal (42), binary with 0b prefix (0b101010), hexadecimal with 0x prefix (0xDEADBEEF), and octal with 0o prefix (0o52). The prefix is case-insensitive. Spaces and underscores within the number are stripped for readability (e.g., 1_000_000 or 0xFF_FF are valid).
The Hamming distance between two integers a and b equals popcount(a XOR b). XOR produces a 1 wherever the bits differ. Counting those 1-bits gives the number of bit positions where a and b disagree. This is the foundation of error-detection codes like Hamming(7,4) and is used in similarity metrics for binary feature vectors in machine learning.