1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

math.big: improve is_power_of_2 (#18914)

This commit is contained in:
phoebe 2023-07-20 12:51:26 +02:00 committed by GitHub
parent 6fef2c2ae8
commit 4daddd3e84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1099,9 +1099,20 @@ fn (x Integer) is_odd() bool {
} }
// is_power_of_2 returns true when the integer `x` satisfies `2^n`, where `n >= 0` // is_power_of_2 returns true when the integer `x` satisfies `2^n`, where `n >= 0`
[inline] [direct_array_access; inline]
pub fn (x Integer) is_power_of_2() bool { pub fn (x Integer) is_power_of_2() bool {
return x.bitwise_and(x - one_int).bit_len() == 0 if x.signum == 0 {
return false
}
// check if all but the most significant digit are 0
for i := 0; i < x.digits.len - 1; i++ {
if x.digits[i] != 0 {
return false
}
}
n := u32(x.digits.last())
return n & (n - u32(1)) == 0
} }
// bit_len returns the number of bits required to represent the integer `a`. // bit_len returns the number of bits required to represent the integer `a`.