 # How to read and display 24 bit two's compliment in decimal form

I am trying to read the data from an ADS1231, which is stored in 24 bit two's compliment. In my code I am shifting in all three bytes, but what would be the correct type of variable to store and then read the data from. Also, how could I convert the two's compliment to a voltage. Thanks.

brett_bender: I am trying to read the data from an ADS1231, which is stored in 24 bit two's compliment. In my code I am shifting in all three bytes, but what would be the correct type of variable to store and then read the data from. Also, how could I convert the two's compliment to a voltage. Thanks.

A long variable type should be the type you should fill your bytes into.

Lefty

Use a long (int32_t) and then extend the sign bit.

majenko:
Use a long (int32_t) and then extend the sign bit.

Could you explain? Should I convert the two’s compliment value into a normal binary value?
What should I add to this to extend the sign bit?

``````nt32_t value = 0;
value = shiftIn(data, sclk, MSBFIRST);
value <<= 8;
value |= shiftIn(data, sclk, MSBFIRST);
value <<= 8;
value |= shiftIn(data, sclk, MSBFIRST);
``````

A two’s complement is a binary value.

``````// Shift in the values
value = shiftIn(data, sclk, MSBFIRST);
value <<= 8;
value |= shiftIn(data, sclk, MSBFIRST);
value <<= 8;
value |= shiftIn(data, sclk, MSBFIRST);

// Sign extend - set bits 24 to 31 the same as bit 23
if (value & 0x0800) {
value |= 0xF000;
}
``````