5.24 · exercise
Bit population census
How many bits of a 64-bit value are set to 1? The starter reads the value in hex (b7a5 in the stdin box). Count them with a loop.
The usual loop: use and to keep only the lowest bit, add it to the count, shift the value right by one, and stop when the value is zero. Each 1 bit reaches position zero exactly once. Pick the shift with care: the hidden inputs include values with the top bit set.
What is checked
- the count of set bits is printed on its own line
- the program exits cleanly
- the count comes from masking and shifting (
and,lsr) - the same holds for other values you do not see, 0 and all 64 bits set among them
specification
stdinb7a5
stdoutprints the right output
exitexits with the right code
sourceuses
andsourceuses
lsrsourcedoes not hardcode the answer
hiddenright output and exit code on 5 more inputs you do not see
We run your program on the input above and on the hidden ones, and compare what it does. Nothing is matched against a stored solution.