5.18 · exercise
bit population census
How many bits of 0xb7a5 are set? Count them with a loop instead of trusting anyone's arithmetic.
The classic walk: mask off the lowest bit with and, add it to the count, shift the value right by one, and stop when nothing is left. Every set bit takes exactly one turn through position zero.
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), not from counting on paper
Specification
args
stdoutprints the expected output
exitexits with the expected code
sourceuses
andsourceuses
lsrsourcecomputes the result (does not hardcode it)
Checked by running your program against expected behavior, never by matching a stored solution.
loading editor...