diff options
| author | Mazdak Farrokhzad <twingoow@gmail.com> | 2019-10-21 22:00:47 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-10-21 22:00:47 +0200 |
| commit | a160258927804710e8732ec1ec655160489f460f (patch) | |
| tree | ee88697ab298a0dce350111134d07bafef9470b5 /src/libcore | |
| parent | aba84894d1879a7429d5bc20d0bfdca3471685de (diff) | |
| parent | d689c709ea6e942de1687d918eaee541610aa86d (diff) | |
| download | rust-a160258927804710e8732ec1ec655160489f460f.tar.gz rust-a160258927804710e8732ec1ec655160489f460f.zip | |
Rollup merge of #65092 - tspiteri:const-is-pow2, r=oli-obk
make is_power_of_two a const function This makes `is_power_of_two` a const function by using `&` instead of short-circuiting `&&`; Rust supports bitwise `&` for `bool` and short-circuiting is not required in the existing expression. I don't think this needs a const-hack label as I don't find the changed code less readable, if anything I prefer that it is clearer that short circuiting is not used. @oli-obk
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/num/mod.rs | 4 |
1 files changed, 2 insertions, 2 deletions
diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index 8f4ade377e3..b4ade704144 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -3757,8 +3757,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.count_ones() == 1 } } |
