mirror of
https://github.com/Chia-Network/chia-blockchain.git
synced 2024-09-11 18:58:59 +03:00
Fix edge case with negative input (#8529)
This commit is contained in:
parent
09e3843b24
commit
b72f2c50af
@ -6,7 +6,7 @@ def truncate_to_significant_bits(input_x: int, num_significant_bits: int) -> int
|
||||
"""
|
||||
x = abs(input_x)
|
||||
if num_significant_bits > x.bit_length():
|
||||
return x
|
||||
return input_x
|
||||
lower = x.bit_length() - num_significant_bits
|
||||
mask = (1 << (x.bit_length())) - 1 - ((1 << lower) - 1)
|
||||
if input_x < 0:
|
||||
|
@ -17,6 +17,8 @@ class TestSignificantBits(unittest.TestCase):
|
||||
assert truncate_to_significant_bits(a, 0) == 0b0
|
||||
a = 0b1000000111
|
||||
assert truncate_to_significant_bits(a, 500) == a
|
||||
a = -0b1000000111
|
||||
assert truncate_to_significant_bits(a, 500) == a
|
||||
a = 0b10101
|
||||
assert truncate_to_significant_bits(a, 5) == a
|
||||
a = 0b10101
|
||||
|
Loading…
Reference in New Issue
Block a user