Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![feature(core_intrinsics)]
- use std::intrinsics;
- fn main() {
- for i in 0..=16 {
- println!("{} -> {}", i, prev_power_of_two(i));
- }
- }
- fn prev_power_of_two(x: u32) -> u32 {
- if x <= 1 {
- return x;
- }
- let z = unsafe { intrinsics::ctlz_nonzero(x) };
- (u32::max_value() >> z + 1) + 1
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement