Bit count : preprocessor magic vs modern C++
Why not use the standard library? #include <bitset> int bits_in(std::uint64_t u) { auto bs = std::bitset<64>(u); return bs.count(); } resulting assembler (Compiled with -O2 -march=native): bits_in(unsigned long): xor eax, eax popcnt rax, rdi ret It is worth mentioning at this point that not all x86 processors have this instruction so (at least with gcc) you … Read more