SBAA588 April   2024 LM73 , LM75B , LM95071 , TMP100 , TMP101 , TMP102 , TMP103 , TMP104 , TMP107 , TMP1075 , TMP108 , TMP112 , TMP114 , TMP116 , TMP117 , TMP121 , TMP122 , TMP123 , TMP124 , TMP126 , TMP144 , TMP175 , TMP1826 , TMP1827 , TMP275 , TMP400 , TMP401 , TMP411 , TMP421 , TMP422 , TMP423 , TMP431 , TMP432 , TMP435 , TMP451 , TMP461 , TMP464 , TMP468 , TMP4718 , TMP75 , TMP75B , TMP75C

 

  1.   1
  2.   Abstract
  3.   Trademarks
  4. 1Introduction
    1. 1.1 2's Complement
      1. 1.1.1 2's Complement Traits
    2. 1.2 Q Format
    3. 1.3 Common Temperature Data Format
    4. 1.4 High Accuracy Temperature Data Format
  5. 2Code Examples
    1. 2.1  16 Bits With Q7 Notation
      1. 2.1.1 Properties
      2. 2.1.2 C Code
    2. 2.2  12-bits With Q4 Notation
      1. 2.2.1 Properties
      2. 2.2.2 C Code
    3. 2.3  13-bits With Q4 Notation (EM=1)
      1. 2.3.1 Properties
      2. 2.3.2 C Code
    4. 2.4  13-bits With Q4 Notation
      1. 2.4.1 Properties
      2. 2.4.2 C Code
    5. 2.5  14-bits With Q6 Notation
      1. 2.5.1 Properties
      2. 2.5.2 C Code
    6. 2.6  TMP182x Formats
      1. 2.6.1 Properties
      2. 2.6.2 C Code
    7. 2.7  14-bits with Q5 Notation
      1. 2.7.1 Properties
      2. 2.7.2 C Code
    8. 2.8  8-bits With No Q Notation
      1. 2.8.1 Properties
      2. 2.8.2 C Code
    9. 2.9  11-bits With Q3 Notation
      1. 2.9.1 Properties
      2. 2.9.2 C Code
    10. 2.10 Devices Without 2's Complement
      1. 2.10.1 Properties
      2. 2.10.2 C Code
  6. 3Other Programming Languages
    1. 3.1 Parsing
    2. 3.2 2's Complement
    3. 3.3 Discard Unused Bits
    4. 3.4 Apply Q format
  7. 4Summary
  8. 5References
  9. 6Appendix: Q App Source Code
  10. 7Appendix: Device Summary Table

2's Complement

  • In a language with explicit type conversions, 2's Complement is handled automatically when casting into the correct data type.
/* C Type Casting */
unsigned char x = 0xFF;
signed char y = x;
signed int z = (signed char) x;
/* x is 255 but both y and z are -1 */

/* C99 fixed width integer types */
uint8_t x = 0xFF;
int8_t y = x;
int8_t z = (int8_t) x;
/* x is 255 but both y and z are -1 */
  • Without type conversion, an IF statement is needed to detect and correct numbers that are supposed to be negative.
    • For a 2's complement number with n bits, values greater than or equal to 2(n-1) are actually negative numbers. Restated, if the 2(n-1) bit is present, then the number is negative. This bit is also known as the sign bit. For an 8-bit number, the sign bit is equal to 0x80 or 0b10000000.
    • To decode negative values, subtract 2n. This is needed to reverse the order of 2's complement negative values. See also Table 1-1.

      • The opposite is also true: when encoding a negative number to a signed binary data type, 2n can be added to obtain the correct hex/binary value.
      • Note that adding or subtracting 2n this way assumes that the underlying data type does not overflow. This is a safe assumption most of the time, because that is not necessary to correct the sign of the data this way if we can type cast to the correct number of bits instead. The underlying data type is likely a C float or similar with at least 32 bits.
    • In Excel, use the IF function and 2n statements for readability. Excel does not have good support for bitwise operations. Subtract 2n to convert a number to the correct negative value. These examples are for 16-bit numbers.
  • In the following Excel example, row 1 illustrates decoding of data received from the sensor while row 2 illustrates encoding data to be sent to the device.
Table 3-3 Excel Example for 2's Complement
A B C
1 FFFF =HEX2DEC(A1) =IF(B1>=2^15,B1-2^16,B1)
2 -1 =IF(A2<0,A2+2^16,A2) =DEC2HEX(B2)
Table 3-4 Excel Calculation Result for 2's Complement
A B C
1 FFFF 65535 -1
2 -1 65535 FFFF
  • In JavaScript and Python, both hex and bitwise operators are available.

    • 0x8000 and 0x10000 are equivalent to 215 and 216 without the use of additional operators.

    • Bitwise AND comparison can be used to check for the presence of a sign bit instead of greater-than-equal-to comparison.

/* JavaScript */
/* decode from 8-bit 2's complement */
function decode(x) {return (x & 0x8000) ? x-0x10000 : x}
let n = decode(0xFFFF)
/* n is -1 */

/* encode to 8-bit 2's complement */
function encode(x) {return (x < 0) ? x+0x10000 : x}
let n = encode(-1).toString(16)
/* n is 'ffff' */
# Python
# decode from 8-bit 2's complement
def decode(x): return x-0x10000 if (x & 0x8000) else x
n = decode(0xFFFF)
# n is -1

# encode to 8-bit 2's complement
def encode(x): return x+0x10000 if (x & 0x8000) else x
n = hex(encode(-1))
# n is '0xffff'