SHARE
TWEET

Untitled

a guest May 23rd, 2019 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #![feature(core_intrinsics)]
  2. use std::intrinsics;
  3.  
  4. fn main() {
  5.     for i in 0..=16 {
  6.         println!("{} -> {}", i, prev_power_of_two(i));
  7.     }
  8. }
  9.  
  10. fn prev_power_of_two(x: u32) -> u32 {
  11.     if x <= 1 {
  12.         return x;
  13.     }
  14.     let z = unsafe { intrinsics::ctlz_nonzero(x) };
  15.  
  16.     (u32::max_value() >> z + 1) + 1
  17. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top