Skip to content

Commit a12788a

Browse files
committed
make is_power_of_two a const function
1 parent 31d75c4 commit a12788a

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/libcore/num/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3749,8 +3749,8 @@ assert!(!10", stringify!($SelfT), ".is_power_of_two());", $EndFeature, "
37493749
```"),
37503750
#[stable(feature = "rust1", since = "1.0.0")]
37513751
#[inline]
3752-
pub fn is_power_of_two(self) -> bool {
3753-
(self.wrapping_sub(1)) & self == 0 && !(self == 0)
3752+
pub const fn is_power_of_two(self) -> bool {
3753+
((self.wrapping_sub(1)) & self == 0) & !(self == 0)
37543754
}
37553755
}
37563756

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// run-pass
2+
3+
const IS_POWER_OF_TWO_A: bool = 0u32.is_power_of_two();
4+
const IS_POWER_OF_TWO_B: bool = 32u32.is_power_of_two();
5+
const IS_POWER_OF_TWO_C: bool = 33u32.is_power_of_two();
6+
7+
fn main() {
8+
assert!(!IS_POWER_OF_TWO_A);
9+
assert!(IS_POWER_OF_TWO_B);
10+
assert!(!IS_POWER_OF_TWO_C);
11+
}

0 commit comments

Comments
 (0)