Hacker News new | ask | show | jobs
by Thiez 1146 days ago
A binary number is a power of two iff only a single bit is set, so pretty trivial to implement.
1 comments

Sure, it's literally self.count_ones() == 1 -- so we just implement count_ones() and ah, well, we could do all this by hand but turns out (fill in name of CPU) has a CPU instruction specifically for this. Rust calls the intrinsic we're about to go write intrinsics::ctpop()

Now we're writing per-ISA intrinsics, what was our goal again? Maybe I was too oblique, this stuff is all rabbit holes is what I was getting at. We're lucky these people even re-surface periodically with work and a blog post.

Or (x & (x-1)) and let the compiler figure that out.