nim-stint icon indicating copy to clipboard operation
nim-stint copied to clipboard

Parser does not left-extend sign for negative numbers when `bitsize < WordBits`

Open arnetheduck opened this issue 2 years ago • 0 comments

https://github.com/status-im/nim-stint/blob/711cda4456c32d3ba3c6c4524135b3453dffeb9c/tests/test_io.nim#L803

When parsing a negative number into a signed stint smaller than the word size, we must left-extend the sign or the general stint representation becomes inconsistent.

The given example works only because cast is used which hides the bug, which otherwise can be observed like this:

doAssert "0b1111111111111111".parse(StInt[16], 2).isNegative()
doAssert "0b1111111111111111".parse(StInt[16], 2).truncate(int16) == -1

arnetheduck avatar Sep 27 '23 07:09 arnetheduck