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 Traits

Table 1-1 3-bit 2's Complement Table
BinarySigned ValueUnsigned Value
0b100-44
0b101-35
0b110-26
0b111-17
0b00000
0b00111
0b01022
0b01133

This table provides the full range of values in a 3-bit 2's complement number. The low bit count enables us to easily see all the possible values and observe common traits in 2's Complement encoding.

Here are some facts about 2's complement to keep in mind:

  • The most significant bit indicates sign.
  • The highest number that can be expressed is 0 followed by all 1's in binary, which is 0b011 shown here.
  • The largest absolute value that can be expressed is 1 followed by all 0's (0b100 here,) but this is always a negative number.
  • All 1's in binary is always equal to -1.
  • Adding +1 to -1 causes rollover to 0, which is the expected mathematical result.

In the following abbreviated table, we can see the same traits apply to an 8-bit 2's Complement encoding. Note that the column Signed Value describes the 8-bit C data type int8_t and the column Unsigned Value describes the 8-bit C data type uint8_t.

Table 1-2 8-bit 2's Complement Table
Binary Hex Signed Value Unsigned Value
0b10000000 0x80 -128 128
0b10000001 0x81 -127 129
... ... ... ...
0b11111101 0xFD -3 253
0b11111110 0xFE -2 254
0b11111111 0xFF -1 255
0b00000000 0x00 0 0
0b00000001 0x01 1 1
0b00000010 0x02 2 2
... ... ... ...
0b01111110 0x7E 126 126
0b01111111 0x7F 127 127