所以我使用的是一个使用 mbed 接口(interface)的 LPC1768。
这段代码:
int16_t test = -1;
test -= 1;
printf("Value: %d\n", sizeof(test));
if (test== 0xFFFE) {
printf("It's stayed the same.\n");
} else if (test== 0xFFFFFFFE) {
printf("It's been extended.\n");
} else {
printf("None\n");
}
打印出来 值:2 它已被扩展。
我如何修改它以便打印“It's stayed the same.”?目标是拥有
int16_t test = -1
使测试成为一个 16 位的值,所有位都已设置。
最佳答案
0xFFFFFFFE
和 0xFFFE
都是 int
常量。前者超出了 int
的范围,因此它溢出到负数(-2),然后 short
操作数被扩展为 int
(与符号扩展)并且比较成功。后者相当于值65534,不等于-2。
要使测试成功,将文字0xFFFE
转换为int16_t
,这将导致它溢出到值-2:
if (test == static_cast<int16_t>(0xFFFE)) {
关于c++ - 如何获得真正的 16 位值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43531624/