__builtin_clz() is negative
__builtin_clz() is negative
我有一些这样的代码:
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
最奇怪的是,这里等于587581823
总是不同的随机数(有时小于 0(__builtin_clz(x1)
并且__builtin_clz(x2)
总是-32
如果您查看 gcc 文档以了解__builtin_ctz
我们有:
未定义内置功能:
int __builtin_ctz (unsigned int x)
返回 x 中尾随 0 位的数,从最低有效位位置开始。如果 x 为 0,则结果未定义。[强调我的]
就是未定义。您看到的结果完全任意的数字完全在"未定义"的范围内。
不能使用值为 0 的__builtin_clz
文档 说:
— 内置函数:int __builtin_clz(无符号 int x(返回 x 中的前导 0 位数,从最高有效位开始 位置。如果 x 为 0,则结果未定义
相关文章:
- Why is UINT32_MAX + 1 = 0?
- C++错误:"error: int aaa::bbb is protected within this context"
- 创建具有 new in 函数和"this is nullptr"异常的对象
- 使用 cmake 的 Linux 终端上的"Conversion to non-scalar type is requested"错误
- Is !NaN not a NaN?
- Directx 11 - CompileFromFile() is not compiling
- Centos7 g++ "to_string is not in a member of std"
- "Unable to start debugging. No process is associated with this object." - 在Visual Studio Code中使用GDB
- Where is std::hardware_destructive_interference_size?
- 将成员函数作为构造函数参数调用时出错 "Variable is not a type name"
- What is unordered_set in C++
- 你如何理解"std: :forward is just syntactic sugar"?这是真的吗?
- C++ "error: use of overloaded operator '*' is ambiguous"似乎只有一场比赛
- C++ is calculating wrong
- Is it good to use SDL_PIXELFORMAT_UNKNOWN?
- reference_wrapper导致"incomplete type is not allowed"
- 为什么即使在定义之后仍存在"Identifier is undefined error "?
- 结构中的错误"Incomplete type is not allowed"
- Crash in boost::archive::text_iarchive in_archive {is} boost
- __builtin_clz() is negative