std: add math.isPowerOfTwo

This commit is contained in:
daurnimator 2019-05-20 23:22:58 +10:00
parent 5017a1d895
commit ebedc99ac1
No known key found for this signature in database
GPG Key ID: 45B429A8F9D9D22A

View File

@ -680,6 +680,11 @@ pub fn alignCast(comptime alignment: u29, ptr: var) AlignCastError!@typeOf(@alig
return @alignCast(alignment, ptr);
}
pub fn isPowerOfTwo(v: var) bool {
assert(v != 0);
return (v & (v - 1)) == 0;
}
pub fn floorPowerOfTwo(comptime T: type, value: T) T {
var x = value;