Reputation: 27
Int64_t n=7;
after printing __builtin_clz(n)
answer is 29
rather than the expected answer 61
.
Upvotes: 0
Views: 277
Reputation: 7482
That is the signature for the intrinsic you are using:
int __builtin_clz (unsigned int x)
As you can see it works on 32-bit unsigned. It treat your 64 bit integer as a 32 one. Since 7 has 4 bits set it return 32-3 = 29
Try __builtin_clzl;
or __builtin_clzll
instead.
Upvotes: 4
Reputation: 85777
https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html states:
Built-in Function:
int __builtin_clz (unsigned int x)
I.e. n
is implicitly converted to an unsigned int
because that's what the function takes.
There's also int __builtin_clzll (unsigned long long)
if you need more bits.
Upvotes: 5