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

Discard Unused Bits

  • Some devices have unused bits on the right (least-significant) side that must be discarded.
  • Discarding is accomplished with right shift (>>) in languages that support bit shift operations.
  • In situations where left shift is needed, or if sign-extend can't be avoided, you can mask unwanted bits to remove them. Masking is best accomplished with bitwise AND. The mask is a constant which has the bits that need to be retained set to logic 1. For example, a mask of 0xF is equivalent to 0b1111, and retains only the four least significant bits.
/* right shift */
unsigned char x = 0x23;
unsigned char y = x >> 4;
/* y is 0x02 */

/* mask to discard bits */
unsigned char z = x & 0xF;
/* z is 0x03 */
  • In Excel, multiply and divide operations can be equivalent to left and right shift. For example, multiply times 23 is equivalent to left shift 3. Right shift can be accomplished with *2-b or /2b. When right shifting this way, the result must be rounded to a whole number to discard the low bits. The INT() function is used here to round down.
  • BITAND() can be used for masking, but beware this takes decimal input and provides decimal output. This can make BITAND() confusing to use for masking. BITAND() works with at least 32 bits, similar to HEX2DEC().
  • The following Excel example demonstrates the same shift and discard operations on 0x23 as illustrated in the other language examples. Note that in this example, Excel is showing the decimal result in Column C. Our result coincidentally appears the same in decimal as the result does if displayed in hex as seen is in the other examples. Row 1 is the shift while row 2 is the discard operation.
Table 3-5 Excel Example for Discarding Bits
ABC
123=HEX2DEC(A1)=INT(B1*2^-4)
223=HEX2DEC(A2)=BITAND(B2,HEX2DEC("F"))
Table 3-6 Excel Calculation Result for Discarding Bits
A B C
1 23 35 2
2 23 35 3
  • JavaScript and Python support shift as well as bitwise AND.
/* JavaScript */
/* right shift */
let x = 0x23
let y = x >> 4
/* y is 0x2 */

/* mask to discard bits */
let z = x & 0xF
/* z is 0x3 */
# Python
# right shift
x = 0x23
y = x >> 4
# y is 0x2

# mask to discard bits
z = x & 0xF
# z is 0x3