We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent d87078b commit 2017cc3Copy full SHA for 2017cc3
src/libstd/num/uint.rs
@@ -101,7 +101,17 @@ pub fn next_power_of_two(n: uint) -> uint {
101
let mut tmp: uint = n - 1u;
102
let mut shift: uint = 1u;
103
while shift <= halfbits { tmp |= tmp >> shift; shift <<= 1u; }
104
- return tmp + 1u;
+ tmp + 1u
105
+}
106
+
107
+/// Returns the smallest power of 2 greater than or equal to `n`
108
+#[inline]
109
+pub fn next_power_of_two_opt(n: uint) -> Option<uint> {
110
+ let halfbits: uint = sys::size_of::<uint>() * 4u;
111
+ let mut tmp: uint = n - 1u;
112
+ let mut shift: uint = 1u;
113
+ while shift <= halfbits { tmp |= tmp >> shift; shift <<= 1u; }
114
+ tmp.checked_add(&1)
115
}
116
117
#[cfg(target_word_size = "32")]
0 commit comments