make is_power_of_two a const function

This commit is contained in:
Trevor Spiteri 2019-08-22 19:57:48 +02:00
parent 31d75c4e9c
commit a12788a0d7
2 changed files with 13 additions and 2 deletions

View File

@ -3749,8 +3749,8 @@ assert!(!10", stringify!($SelfT), ".is_power_of_two());", $EndFeature, "
```"),
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn is_power_of_two(self) -> bool {
(self.wrapping_sub(1)) & self == 0 && !(self == 0)
pub const fn is_power_of_two(self) -> bool {
((self.wrapping_sub(1)) & self == 0) & !(self == 0)
}
}

View File

@ -0,0 +1,11 @@
// run-pass
const IS_POWER_OF_TWO_A: bool = 0u32.is_power_of_two();
const IS_POWER_OF_TWO_B: bool = 32u32.is_power_of_two();
const IS_POWER_OF_TWO_C: bool = 33u32.is_power_of_two();
fn main() {
assert!(!IS_POWER_OF_TWO_A);
assert!(IS_POWER_OF_TWO_B);
assert!(!IS_POWER_OF_TWO_C);
}