Price your future right. 60% off with code BLACKSCHOLES at USD checkout. Offer ends in 1d 16h 01m 48s1 days 16 hours 1 minutes 48 seconds.
Price your future right. 60% off with code BLACKSCHOLES at USD checkout. Offer ends in 1d 16h 01m 48s1 days 16 hours 1 minutes 48 seconds.

Sign in to watch this lesson
Algorithms - Bits and Number Theory · 4:44
An integer is a row of bits you can edit in one instruction: place values, two's complement (-13 = 1111 0011), shifts as multiplying and dividing by powers of two (and why -13 >> 1 is -7 but -13 / 2 is -6), and the mask idioms that set, clear, flip and test a bit. Then the trick behind most bit hacks, n & (n - 1), which clears the lowest set bit and gives the power-of-two test and Kernighan's popcount, plus C++20's <bit> functions and the pitfalls of 1 << 31 and __builtin_popcount on 64-bit values. It ends by counting the ones in 1..n in O(log n) by peeling off the top bit.