Last modified: 2020-07-03
This is a web page of
David Casperson
Associate Professor
Computer Science
University of Northern British Columbia

About Computer Science…test

construction image

This page is under construction.

This page is a test.

Binary Numbers

How to count the number of ones in a binary number

A number like 13 is 1101 in binary. The sum of the binary digits is three. This is easy enough to see. Can we compute this without first converting a number from decimal to binary? Yes!

One technique is based on dividing by two and rounding down. 13 divided by two is 6.5, which rounded down is 6. Next we get 3, then 1, then 0.

We can turn this into one long substraction 13-6-3-1-0, which is 3. This is the number of ones in the binary representation of 13 (1101).

published