about summary refs log tree commit diff
path: root/src/libstd/num/mod.rs
diff options
context:
space:
mode:
authorClark Gaebel <cg.wowus.cg@gmail.com>2014-03-26 21:58:08 -0400
committerClark Gaebel <cg.wowus.cg@gmail.com>2014-04-18 20:15:41 -0400
commit9f45484db5cc4570b4e489e82646efc5c628b6ea (patch)
tree54102b238bcabee223a61d96c754e23d0360fde2 /src/libstd/num/mod.rs
parentb75683cadf6c4c55360202cd6a0106be80532451 (diff)
downloadrust-9f45484db5cc4570b4e489e82646efc5c628b6ea.tar.gz
rust-9f45484db5cc4570b4e489e82646efc5c628b6ea.zip
Reduce HashMap allocations.
Diffstat (limited to 'src/libstd/num/mod.rs')
-rw-r--r--src/libstd/num/mod.rs6
1 files changed, 6 insertions, 0 deletions
diff --git a/src/libstd/num/mod.rs b/src/libstd/num/mod.rs
index 12befed743a..b21a80cfd23 100644
--- a/src/libstd/num/mod.rs
+++ b/src/libstd/num/mod.rs
@@ -311,6 +311,12 @@ pub fn next_power_of_two<T: Unsigned + Int>(n: T) -> T {
     tmp + one()
 }
 
+// Returns `true` iff `n == 2^k` for some k.
+#[inline]
+pub fn is_power_of_two<T: Unsigned + Int>(n: T) -> bool {
+    (n - one()) & n == zero()
+}
+
 /// Returns the smallest power of 2 greater than or equal to `n`. If the next
 /// power of two is greater than the type's maximum value, `None` is returned,
 /// otherwise the power of 2 is wrapped in `Some`.