|
|
|
|
|
by nickysielicki
1839 days ago
|
|
Flipping the order works, except if the LSB on x is set. https://godbolt.org/z/qdWhxMPsf Note the run output under clang. edit: > And the LLVM source indicate to me it only picks up on x&(x-1) pattern, which would miss the popcount optimization on code like mine. Thanks for teaching me something this morning. That's annoying. I think the portable solution is std::popcount in C++ (or equivalent in Rust). |
|
Example with int8_t:
One needs to be careful when using >> (shift right) with signed integers.So your program is not equivalent to popcount.