Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // returns index of most significant '1' bit in a number
- pub fn msb_idx(n: u64) -> u8 {
- let mut idx: u8 = 0;
- let mut tmp: u64 = n;
- while tmp != 0 {
- tmp >>= 1;
- idx += 1;
- }
- idx - 1
- }
- // returns index of the least significant '1' bit in a number
- pub fn lsb_idx(n: u64) -> u8 {
- let lsb: u64 = n ^ (n & (n - 1)); // isolate the LSB
- msb_idx(lsb) // the isolated LSB is now also the MSB
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement