diff options
Diffstat (limited to 'lib/std/math.zig')
| -rw-r--r-- | lib/std/math.zig | 19 |
1 files changed, 16 insertions, 3 deletions
diff --git a/lib/std/math.zig b/lib/std/math.zig index e60e964747..e62a208d0b 100644 --- a/lib/std/math.zig +++ b/lib/std/math.zig @@ -1111,9 +1111,22 @@ pub fn alignCast(comptime alignment: u29, ptr: anytype) AlignCastError!@TypeOf(@ return @alignCast(alignment, ptr); } -pub fn isPowerOfTwo(v: anytype) bool { - assert(v != 0); - return (v & (v - 1)) == 0; +/// Asserts `int > 0`. +pub fn isPowerOfTwo(int: anytype) bool { + assert(int > 0); + return (int & (int - 1)) == 0; +} + +test isPowerOfTwo { + try testing.expect(isPowerOfTwo(@as(u8, 1))); + try testing.expect(isPowerOfTwo(2)); + try testing.expect(!isPowerOfTwo(@as(i16, 3))); + try testing.expect(isPowerOfTwo(4)); + try testing.expect(!isPowerOfTwo(@as(u32, 31))); + try testing.expect(isPowerOfTwo(32)); + try testing.expect(!isPowerOfTwo(@as(i64, 63))); + try testing.expect(isPowerOfTwo(128)); + try testing.expect(isPowerOfTwo(@as(u128, 256))); } /// Aligns the given integer type bit width to a width divisible by 8. |
