对于min(ctz(x), ctz(y))
,我们可以使用ctz(x | y)
来获得更好的性能.但是max(ctz(x), ctz(y))
呢?
ctz
表示"计数尾随零".
C++版本(Compiler Explorer)
#include <algorithm>
#include <bit>
#include <cstdint>
int32_t test2(uint64_t x, uint64_t y) {
return std::max(std::countr_zero(x), std::countr_zero(y));
}
铁 rust 版(Compiler Explorer)
pub fn test2(x: u64, y: u64) -> u32 {
x.trailing_zeros().max(y.trailing_zeros())
}