~/lightning

Lightning Talk: Finding Whether a Number is a Power of 2

Lightningat CppCon 2022 · 2022-09-11..09-16· [javascript]
links:none on file
slides:none on file

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

  1. 0:00Interview question
  2. 0:30Powers of two in binary
  3. 1:15Bit counting approach
  4. 2:24Bit manipulation trick
  5. 3:13Using std::bitset
  6. 3:40Modern C++20 solutions
  7. 4:31Performance and conclusion
~/search
type to search 6,249 conferences, 138,987 speakers and 185,172 talks
~/help
cmdctrl k
search every conference, speaker and series
/
focus the filter search
h
home
c
conferences
s
speakers
e
series
t
light / dark — the status bar button does the same
?
this help
esc
close this, then clear every filter — or just the search, while you are typing in it

Ignored while you are typing. Pages without a filter rail have no search to focus.