| ... | @@ -1111,9 +1111,22 @@ pub fn alignCast(comptime alignment: u29, ptr: anytype) AlignCastError!@TypeOf(@ | ... | @@ -1111,9 +1111,22 @@ pub fn alignCast(comptime alignment: u29, ptr: anytype) AlignCastError!@TypeOf(@ |
| 1111 | return @alignCast(alignment, ptr); | 1111 | return @alignCast(alignment, ptr); |
| 1112 | } | 1112 | } |
| 1113 | | 1113 | |
| 1114 | pub fn isPowerOfTwo(v: anytype) bool { | 1114 | /// Asserts `int > 0`. |
| 1115 | assert(v != 0); | 1115 | pub fn isPowerOfTwo(int: anytype) bool { |
| 1116 | return (v & (v - 1)) == 0; | 1116 | assert(int > 0); |
| | 1117 | return (int & (int - 1)) == 0; |
| | 1118 | } |
| | 1119 | |
| | 1120 | test isPowerOfTwo { |
| | 1121 | try testing.expect(isPowerOfTwo(@as(u8, 1))); |
| | 1122 | try testing.expect(isPowerOfTwo(2)); |
| | 1123 | try testing.expect(!isPowerOfTwo(@as(i16, 3))); |
| | 1124 | try testing.expect(isPowerOfTwo(4)); |
| | 1125 | try testing.expect(!isPowerOfTwo(@as(u32, 31))); |
| | 1126 | try testing.expect(isPowerOfTwo(32)); |
| | 1127 | try testing.expect(!isPowerOfTwo(@as(i64, 63))); |
| | 1128 | try testing.expect(isPowerOfTwo(128)); |
| | 1129 | try testing.expect(isPowerOfTwo(@as(u128, 256))); |
| 1117 | } | 1130 | } |
| 1118 | | 1131 | |
| 1119 | /// Aligns the given integer type bit width to a width divisible by 8. | 1132 | /// Aligns the given integer type bit width to a width divisible by 8. |