1
我在调试一个问题,并发现下面的代码片段(消毒)是问题的原因。按位不是有符号整数和无符号整数在C
uint64_t testflag = 0;
testflag &= ~(0x08ul); //bug
testflag &= ~(0x08l); //expected
我比较生成的程序集和看到这
uint64_t testflag = 0;
804851e: c7 45 d8 00 00 00 00 movl $0x0,-0x28(%ebp)
8048525: c7 45 dc 00 00 00 00 movl $0x0,-0x24(%ebp)
testflag &= ~(0x08ul);
804852c: 83 65 d8 f7 andl $0xfffffff7,-0x28(%ebp)
8048530: 83 65 dc 00 andl $0x0,-0x24(%ebp)
testflag &= ~(0x08l);
8048534: 83 65 d8 f7 andl $0xfffffff7,-0x28(%ebp)
8048538: 83 65 dc ff andl $0xffffffff,-0x24(%ebp)
为什么的unsigned long
的NOT operator
使编译器和0
具有较高的字节,而不是ffffffff
。
在64位机器上,我的gcc版本是gcc (GCC) 4.9.2 (Red Hat 4.9.2)
。
什么是'sizeof(unsigned long)'? – aschepler
签名扩展名与零扩展名,因为在您的机器上long是32位。 –