Error in ch4 - primitive types

#1

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

0 Likes

#2

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.

0 Likes

#3

Oh, you’re right. Thanks!

0 Likes