C按位运算符`~`

时间:2018-06-07 02:41:07

标签: c

我是C的新手,并使用~(一个补码)一元运算符来查找unsigned char的上限。

printf("Maximum Unsigned Char %d\n", (unsigned char) ~0);
// output:
// Maximum Unsigned Char 255

// when I try to write it in another way:
unsigned char t = 0;
printf("Maximum Unsigned Char %d\n", ~t);
// output:
// Maximum Unsigned Char -1

我对不同的输出感到困惑。因为我已明确将t定义为unsigned char所以我期望255但得到-1

0 个答案:

没有答案