Error in ch4 - primitive types


Not sure what edition I have - just purchased it from Amazon 2/8/2019.

Unless I’m mistaken, the code on page 23 has an error. The code is supposed to return the number of bits in a number passed to the func count_bits, but it in fact returns the number of bits of value 1.

The fourth line should be:
num_bits += 1
Instead of:
num_bits += x & 1

Can someone pls confirm? Thx


No this is correct: The book says: ‘Writing a program to count the number of bits that are set to 1 in a nonnegative integer…’

So it should only count the integers that are actually set to 1.


Oh, you’re right. Thanks!