ZHCADY9A April 2024 – January 2025 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 */
要解码负值,请减去 2n。这是为了将二进制补码负值的顺序颠倒过来。另请参阅表 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 |
JavaScript 和 Python 都支持使用十六进制和按位运算符。
0x8000 和 0x10000 相当于 215 和 216,无需使用额外的运算符。
按位与比较可用于检查是否存在符号位,而不是进行大于等于的比较。
/* 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'