Count the 1-bits
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version.
Count the 1-bits
Every unsigned integer is stored in binary. Return how many of its bits are 1 (this is called the population count). Look at the lowest bit with x & 1u, then shift x right by one with x >>= 1, until x becomes 0.
Complete int popcount(unsigned x) to return the number of 1-bits in x. Check the lowest bit with x & 1u, then shift right with x >>= 1.
Click Run to see the output here.