简体   繁体   English

clang 的 32 位内置人口计数 long long integer c++

[英]32 bit builtin population count for clang counts long long integer c++

I was using the __builtin_popcount with clang compiler and I needed to count a 64 bit number ( unsigned long long or uint64_t ).我将__builtin_popcount与 clang 编译器一起使用,我需要计算一个 64 位数字( unsigned long longuint64_t )。 From looking it up, __builtin_popcount counts 16 bits, __builtin_popcountl counts 32 bits, and __builtin_popcountll counts 64 bits.查了一下, __builtin_popcount为 16 位, __builtin_popcountl为 32 位, __builtin_popcountll为 64 位。 When I tested it, __builtin_popcountl was able to do calculations on 64 bit integers.当我测试它时, __builtin_popcountl能够对 64 位整数进行计算。 Does anybody know the reason for this?有人知道这是什么原因吗?

#include <iostream>

int main() {
    unsigned long long bb = 0b1000000100000001000000010000000100000001000000010000000100000001;
    std::cout << __builtin_popcountl(bb) << std::endl; //returns 9 (correct answer)
}

int __builtin_popcountl (unsigned long) is for unsigned long s. int __builtin_popcountl (unsigned long)用于unsigned long s。
int __builtin_popcountll (unsigned long long) is for unsigned long long s. int __builtin_popcountll (unsigned long long)用于unsigned long long s。

unsigned long is 64 bit on your platform, so the conversion from unsigned long long to unsigned long is lossless, and you can use __builtin_popcountl for 64 bit numbers too. unsigned long在您的平台上是 64 位的,因此从unsigned long longunsigned long的转换是无损的,您也可以将__builtin_popcountl用于 64 位数字。

int is guaranteed to be 16 bits or wider, long is guaranteed to be 32 bits or wider, and long long is guaranteed to be 64 bits or wider. int保证为 16 位或更宽, long保证为 32 位或更宽, long long保证为 64 位或更宽。 That means you can always use __builtin_popcountl with 32 bit numbers, and you may or may not be able to use it with 64 bit numbers (in this case you could).这意味着您始终可以将__builtin_popcountl与 32 位数字一起使用,并且您可能会或可能无法将其与 64 位数字一起使用(在这种情况下您可以)。

Related question: What is the bit size of long on 64-bit Windows?相关问题: 64 位 Windows 上 long 的位大小是多少?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM