math.big: fix Integer.bit_len() when there are no digits in the number
parent
055c33e2ca
commit
3bae3a23df
|
@ -433,7 +433,7 @@ fn test_set_bit() {
|
||||||
assert a == b
|
assert a == b
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_bit_len() {
|
fn test_bit_len() ? {
|
||||||
assert big.zero_int.bit_len() == 0
|
assert big.zero_int.bit_len() == 0
|
||||||
assert big.one_int.bit_len() == 1
|
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.one_int.lshift(1239).bit_len() == 1240
|
||||||
|
|
||||||
assert big.integer_from_string('4338476092346017364013796407961305761039463198075691378460917856') or {
|
mut num := big.integer_from_string('4338476092346017364013796407961305761039463198075691378460917856')?
|
||||||
panic('Could not read from decimal')
|
assert num.bit_len() == 212
|
||||||
}.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 {
|
if x.signum == 0 {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
if x.digits.len == 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
return x.digits.len * 32 - bits.leading_zeros_32(x.digits.last())
|
return x.digits.len * 32 - bits.leading_zeros_32(x.digits.last())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue