相关疑难解决方法(0)

返回64位整数中所有设置位的最快方法是什么?

我需要一种快速的方法来获取64位整数中所有位的位置.例如,给定x = 123703,我想填充一个数组idx[] = {0, 1, 2, 4, 5, 8, 9, 13, 14, 15, 16}.我们可以假设我们先验地知道比特数.这将被称为10 ^ 12 - 10 ^ 15次,因此速度至关重要.到目前为止,我提出的最快答案是以下怪物,它使用64位整数的每个字节作为表的索引,这些表给出了在该字节中设置的位数以及这些位的位置:

int64_t x;            // this is the input
unsigned char idx[K]; // this is the array of K bits that are set
unsigned char *dst=idx, *src;
unsigned char zero, one, two, three, four, five;  // these hold the 0th-5th bytes
zero  =  x & 0x0000000000FFUL;
one   = (x & 0x00000000FF00UL) >> 8;
two   = (x & …
Run Code Online (Sandbox Code Playgroud)

c c++ optimization performance bit-manipulation

40
推荐指数
3
解决办法
4877
查看次数

标签 统计

bit-manipulation ×1

c ×1

c++ ×1

optimization ×1

performance ×1