C 关于unsigned int compzero = ~0;与unsigned int compzero = 0xFFFF; 的区别!

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了。

猜你喜欢

转载自blog.csdn.net/weibo1230123/article/details/82822479