我正在处理一些操作,并且将相同行代码的奇怪的不同输出放入宏和内联函数中。该函数返回具有来自N
的L
活动位的64位掩码,该掩码具有第(~0<<N) - (~0<<(N+L))
位。有人能告诉我为什么产量不同吗?shift count> =类型和宏的宽度vs内联函数
#include <iostream>
#include <bitset>
using namespace std;
#define ONES (~0UL)
#define MASK(from_bit, nbits) \
(ONES << (from_bit)) - (ONES << ((from_bit) + (nbits)))
inline unsigned long int mask(size_t from_bit, size_t nbits) {
return (ONES << from_bit) - (ONES << (from_bit + nbits));
}
int main(int argc, char **argv) {
cout << "using #define: " << bitset<64>(MASK(63, 3)) << endl;
cout << "using inline function: " << bitset<64>(mask(63, 3)) << endl;
return 0;
}
输出:
$ g++ -o test main.cc
main.cc: In function 'int main(int, char**)':
main.cc:15: warning: left shift count >= width of type
$ ./test
using #define: 1000000000000000000000000000000000000000000000000000000000000000
using inline function: 1000000000000000000000000000000000000000000000000000000000000100
------^
与-O3
选项编译:
$ g++ -O3 -o test2 main2.cc
main.cc: In function 'int main(int, char**)':
main.cc:15: warning: left shift count >= width of type
$ ./test2
using #define: 1000000000000000000000000000000000000000000000000000000000000000
using inline function: 0000000000000000000000000000000000000000000000000000000000000000
------^
编译器的信息:
$ g++ --version
i686-apple-darwin11-llvm-g++-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.9.00)
可能是因为你在超过64位的移位(最多)64位类型,导致不确定的行为。注意你的编译器警告! – Yuushi 2013-04-10 10:54:02
所以溢出左移并不会自动删除位,因为我认为... – pckben 2013-04-10 11:06:19