math.big: fix Integer.bit_len() when there are no digits in the number
parent
ebac3bebb1
commit
ed17779434
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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())
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue