Home Tools Blog About

Count Set Bits: Hamming Weight Explained

Counting set bits means counting how many 1s are in a number’s binary form, a value also called the Hamming weight or population count. The byte 10110100 has four 1s, so its set-bit count is 4. This guide explains how to count set bits, the difference between counting ones and zeros, why the count is useful, and free tools that do it for any number.

What a set bit is

A bit is “set” when it is 1 and “clear” when it is 0. The set-bit count, or Hamming weight, is simply how many positions hold a 1. For the byte 10110100, the 1s sit in four positions, so the weight is 4. The zero count is whatever is left over from the total width, here 8 minus 4, which is 4 zeros. If the idea of bit positions is new, our number systems guide sets it up.

How to count the 1s

By hand you just scan the binary string and tally the 1s. The catch is that you need the binary form first, and a decimal number hides it. Converting 180 to binary gives 10110100, and then the four 1s are easy to count. The count one bits tool takes the number directly and returns the weight, so you skip the conversion step.

Counting zeros too

Counting zeros is the mirror image, but it depends on the width you choose. The number 5 is 101 in three bits with one zero, but 00000101 in eight bits with six zeros. That is why a zero count is only meaningful against a fixed width such as a byte or a 16-bit word. The count zero bits tool lets you set the width so the answer matches your context.

Faster ways machines count

Scanning every bit works but is slow for a CPU doing it millions of times, so processors use a dedicated popcount instruction or a bit trick that clears the lowest set bit each loop and counts the iterations. You do not need the trick to count by hand, but it explains why population count is treated as a single fast operation in performance-sensitive code.

Why the count matters

Set-bit counts appear all over computing. A subnet mask’s weight is the prefix length, so 11111111 11111111 11111111 00000000 has a weight of 24, written as a /24 network. Error-detecting codes compare Hamming weights to spot flipped bits. Permission and feature flags use the count to see how many options are enabled. It is a small number that answers a surprising range of questions, and it pairs naturally with the bitwise operations in our bitwise guide.

Free tools used in this guide

Frequently asked questions

What does counting set bits mean?

It means counting how many 1s are in a number’s binary form. This value is also called the Hamming weight or population count.

How many set bits are in the byte 10110100?

Four. The 1s sit in four of the eight positions, so the set-bit count is 4 and the zero count is also 4.

Why does the zero count depend on width?

Because leading zeros only exist relative to a chosen width, so the same value has more zeros in a 16-bit word than in an 8-bit byte.

What is Hamming weight used for?

For subnet prefix lengths, error-detecting codes, and counting how many flags or options are enabled in a value.

Do I need the binary form to count set bits?

Yes, the count is about the bits, so a decimal number must be read in binary first. A count tool does the conversion for you.

ATV

Written by Nick (ATV Team)

We build and maintain the 600+ free, client-side tools on this site, and every guide is written against the tools themselves: each figure is computed and checked before it is published, and every linked tool is tested in the browser. More about how we work on the about page, and the full library of guides lives on the blog.