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
/* 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 */
To decode negative values, subtract 2n. This is needed to reverse the order of 2's complement negative values. See also Table 1-1.
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) |
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'