4 ms·
A binary number is a power of two iff only a single bit is set, so pretty trivial to implement.
by Thiez 3y ago
A binary number is a power of two iff only a single bit is set, so pretty trivial to implement.
- tialaramex 3y agoSure, 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.
- barsonme 3y agoOr (x & (x-1)) and let the compiler figure that out.