考虑这段代码:
uint16_t a = ~ ( uint16_t ) 0;
int16_t b = ~ ( int16_t ) 0;
printf (
"%d %d %d %d\n",
a == ~ ( uint16_t ) 0,
a == ( uint16_t ) ( ~ ( uint16_t ) 0 ),
b == ~ ( int16_t ) 0,
b == ( int16_t ) ( ~ ( int16_t ) 0 )
);
输出是:
0 1 1 1
GCC 发出警告a == ~ ( uint16_t ) 0
:
由于数据类型的范围有限,比较总是错误的 [-Wtype-limits]
为什么按位“非”运算符试图返回有符号值?我怎样才能防止这种情况?