C 关于unsigned int compzero = ~0;与unsigned int compzero = 0xFFFF; 的区别!
unsigned int zero = 0;
unsigned int compzero = 0xFFFF;
对于一个int型不是16位的处理器为说,上面的代码是不正确的。应编写如下:
unsigned int compzero = ~0;
unsigned int compzero = 0xFFFF; 只写了2个字节,16位的才符合 。
32位的可以写:
unsigned int compzero = 0xFFFFFFFF;
但unsigned int compzero = ~0;更安全,不管有多少位,直接取反,把所有的0都变成1了。