Closed umeat closed 3 years ago
Thanks for the report.
Clearly there needs to be more testing around bitpacking, something that has been a bit neglected up till now. I’ll try to add tests that ensure sign extension occurs correctly with different circumstances when I get a chance.
I haven't looked at how parsing these ints is currently implemented, but after unpacking into the int could do something like (say with int64):
// where bits is the number of bits containing the int and val is the int64 which the bits have been unpacked into
if bits < 64 {
if val & math.Pow(2, bits-1) == math.Pow(2, bits-1) {
val = val - math.Pow(2, bits)
}
}
@umeat I found a couple of bugs with bitpacking and tried to resolve them. Please take a look at PR #47 which I plan on merging as long as it seems to work.
Integers which don't sit on byte boundaries aren't correctly parsed as two's complement.
If you have a int4 which is "1111" (that's -1 converted), restruct seems to unpack this as "00001111" which is +15 converted.
An example of this in practice can be found here: https://github.com/go-gnss/rtcm/blob/master/rtcm3/antenna.go#L8. The Int64 values in the AntennaReferencePoint type are never correct if the value is supposed to be negative.
If you checkout the code and add the following function to messages_test.go you can see the effect:
This produces the following result:
The values from the bit shifting are correct.