Lightning Talk: Finding Whether a Number is a Power of 2
summary
Several concrete checks appear: a loop that counts ones by rotating, generating candidates and testing equality, the classic n & (n-1) == 0 trick from K&R, and using a std::bitset count (which is not constexpr). C++20 adds std::popcount and std::has_single_bit; popcount often compiles to a single CPU instruction so it wins on performance, and has_single_bit returns a direct yes/no answer.
AI-written summary · about
chapters (7)
- 0:00Interview question
- 0:30Powers of two in binary
- 1:15Bit counting approach
- 2:24Bit manipulation trick
- 3:13Using std::bitset
- 3:40Modern C++20 solutions
- 4:31Performance and conclusion








