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位。 –