Returns power of 2 that's nearest to the number, rounded up
(n: u16)
| 43 | |
| 44 | /// Returns power of 2 that's nearest to the number, rounded up |
| 45 | fn nearest_power_of_two(n: u16) -> Option<u8> { |
| 46 | let powers: Vec<u16> = vec![1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024]; |
| 47 | for (i, p) in powers.iter().enumerate() { |
| 48 | if *p >= n { |
| 49 | return Some(i as u8); |
| 50 | } |
| 51 | } |
| 52 | None |
| 53 | } |
| 54 | |
| 55 | /// Converts a number from decimal to binary, represented as a vector |
| 56 | /// of u8 |