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

math.big: fix Integer.bit_len() when there are no digits in the number

This commit is contained in:
Delyan Angelov 2022-05-18 08:29:08 +03:00
parent ebac3bebb1
commit ed17779434
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 10 additions and 5 deletions

View File

@ -433,7 +433,7 @@ fn test_set_bit() {
assert a == b
}
fn test_bit_len() {
fn test_bit_len() ? {
assert big.zero_int.bit_len() == 0
assert big.one_int.bit_len() == 1
@ -441,7 +441,10 @@ fn test_bit_len() {
assert big.one_int.lshift(1239).bit_len() == 1240
assert big.integer_from_string('4338476092346017364013796407961305761039463198075691378460917856') or {
panic('Could not read from decimal')
}.bit_len() == 212
mut num := big.integer_from_string('4338476092346017364013796407961305761039463198075691378460917856')?
assert num.bit_len() == 212
for num.bit_len() > 0 {
num = num.rshift(8)
assert true
}
}

View File

@ -960,6 +960,8 @@ pub fn (x Integer) bit_len() int {
if x.signum == 0 {
return 0
}
if x.digits.len == 0 {
return 0
}
return x.digits.len * 32 - bits.leading_zeros_32(x.digits.last())
}