c++ - __builtin_clz() 为负

标签 c++ built-in

我有这样的代码:

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/

相关文章:

c++ - 成员未在范围内声明?

javascript - 是否可以用自定义函数覆盖 window.location 函数?

vim - 如何禁用vim中的内置命令

c++ - 使用 __builtin_expect() 或 Linux 内核时 "very likely"的可能性和可能性是多少

python - 使用pdb调试时可以查看python内置函数的源代码吗?

C++ linux 代理服务器通信

c++ - 编译 Boost.Bind 时出错

c++ - 是否有像管道一样工作的 C++ STL 类?

c++ - C++ 中的 STL : no match for 'operator*'

Java内置类到接口(interface)