【问题标题】:C++ gcc _builtin function gives unexpected answerC++ gcc _builtin 函数给出了意想不到的答案
【发布时间】:2017-07-25 12:43:54
【问题描述】:

Int64_t n=7; 打印后__builtin_clz(n) 的答案是29,而不是预期的答案61

【问题讨论】:

标签: gcc c++14 built-in


【解决方案1】:

https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html 状态:

内置函数:int __builtin_clz (unsigned int x)

n 被隐式转换为 unsigned int,因为这是函数所采用的。

如果您需要更多位,还有int __builtin_clzll (unsigned long long)

【讨论】:

    【解决方案2】:

    这是您正在使用的内在函数的签名:

    int __builtin_clz (unsigned int x)

    如您所见,它适用于 32 位无符号。它将您的 64 位整数视为 32 位整数。由于 7 设置了 4 位,因此它返回 32-3 = 29

    改用__builtin_clzl;__builtin_clzll

    details here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-20
      • 2012-10-01
      • 2011-11-29
      • 2014-06-22
      • 1970-01-01
      • 1970-01-01
      • 2020-10-03
      相关资源
      最近更新 更多