【发布时间】:2017-03-28 23:47:36
【问题描述】:
我正在调试一个问题,并发现以下 sn-p(sanitized) 是问题的原因。
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 进行AND。
我的 gcc 版本是 gcc (GCC) 4.9.2 (Red Hat 4.9.2) 在 64 位机器上。
【问题讨论】:
-
什么是
sizeof(unsigned long)? -
符号扩展与零扩展,因为 long 在您的机器上是 32 位。