I want to decode a GPS navigation message where some parameters are marked such that:
Parameters so indicated shall be two's complement, with the sign bit (+ or -) occupying the MSB
For example, I want to store a parameter af0
which has 22 number of bits, with bit 22 as the MSB.
The parameter af0
has been decoded by me and now I need to perform the two's complement operation. I stored af0
using an uint32_t
integer type.
There are also other parameters like IDOT
which has 14 number of bits and I stored it using an uint16_t
.
I'm not sure, but if I understand it correctly if have to check the MSB for 1 or 0. If it is 1 I can
simply calculate the two's complement by negation (and casting) of the value, i.e. int32_t af0_i = -(int32_t)af0
. If the MSB is 0 I just cast the value according: int32_t af0_i = (int32_t)af0
.
Is this correct for uintX_t
integer types? I also tried out: https://stackoverflow.com/a/34076866/6518689 but it didn't fixed my problem, the value remains the same.