在计算有符号和无符号的 char、short、int 和 long 变量范围的过程中,我采用了以下解决方案:
根据解决方案 1,我希望在下面的代码中输出 -1 和 65535,假设它的行为与两个格式说明符的代码(unsigned short)~0
相同。(unsigned int)~0
// the two statements below produce different results
printf("Value of unsigned int is %d\n", (unsigned int)~0); // outputs -1
printf("Value of unsigned int is %u\n", (unsigned int)~0); // outputs 4294967295
// whereas, the two statements below produce the same result. Why?
printf("Value of short unsigned int is %d\n", (unsigned short)~0); // outputs 65535, expected -1
printf("Value short unsigned int is %u\n", (unsigned short)~0); // outputs 65535
(unsigned short)~0
为什么和的行为有所不同(unsigned int)~0
?