Skip to content

Commit

Permalink
make is_power_of_two a const function
Browse files Browse the repository at this point in the history
  • Loading branch information
tspiteri committed Oct 4, 2019
1 parent 31d75c4 commit a12788a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/libcore/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/test/ui/consts/const-int-pow-rpass.rs
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit a12788a

Please sign in to comment.