我有这样的代码:
int foo(unsigned long long x) {
unsigned int x1 = (unsigned int)(x >> 32);
unsigned int x2 = (unsigned int)(x);
if (x == 0) {
cout << x1 << " " << __builtin_clz(x1) << endl;
cout << x2 << " " << __biultin_clz(x2) << endl;
}
}
x = 0
的输出是:
0 587581823
0 -32
最奇怪的是 __builtin_clz(x1)
这里等于 587581823
总是不同的随机数(有时小于 0)和 __builtin_clz (x2)
总是 -32
最佳答案
如果您查看 gcc documentation对于 __builtin_ctz
我们有:
Built-in Function:
int __builtin_ctz (unsigned int x)
Returns the number of trailing 0-bits in x, starting at the least significant bit position. If x is 0, the result is undefined. [emphasis mine]
未定义是未定义的。您看到的完全任意的结果数字完全在“未定义”的范围内。
关于c++ - __builtin_clz() 为负,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27583053/