mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-24 07:44:10 +00:00
make is_power_of_two a const function
This commit is contained in:
parent
31d75c4e9c
commit
a12788a0d7
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
11
src/test/ui/consts/const-int-pow-rpass.rs
Normal file
11
src/test/ui/consts/const-int-pow-rpass.rs
Normal 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);
|
||||
}
|
Loading…
Reference in New Issue
Block a user