x and x minus one
transcript
One line clears the lowest one bit of a number. It is x AND x minus one. It counts bits and tests for a power of two.
An odometer goes from four thousand to three nine nine nine. Binary does the same. Forty four minus one is forty three. The lowest one became zero. The zeros below it became ones. AND keeps what did not change. The lowest one is gone.
Repeat it. Forty becomes thirty two. Thirty two becomes zero. Three steps in all. Forty four has three one bits. One step for each.
A power of two has one bit. So one step reaches zero. Check that x is above zero too.
Languages have a built in count of one bits. Java has Integer dot bitCount. C plus plus has popcount. Python has bit underscore count, from version three ten. A negative number needs care. Java counts thirty one bits in minus five.
Same rule, any input. Full breakdown in the lecture.