c++ - 复制整数位的最快方法

标签 c++ c optimization bit-manipulation bit

复制整数位的最快方法是什么。

例如,

17 -> 10001

复制后: 1100000011

最佳答案

看起来像是位交错的变体。

Interleave bits the obvious way

(modified from http://graphics.stanford.edu/~seander/bithacks.html)

unsigned int x = 17;
unsigned int z = 0; // z gets the resulting Morton Number.

for (int i = 0; i < sizeof(x) * CHAR_BIT; i++) // unroll for more speed...
{
  z |= (x & 1U << i) << i | (x & 1U << i) << (i + 1);
}

参见 http://graphics.stanford.edu/~seander/bithacks.html#InterleaveTableObvious更多方法。

关于c++ - 复制整数位的最快方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55348076/

相关文章:

比较c中的命令行参数

android - 代码优化。 (建筑学)

c++ - 带有常量的可变参数模板

c++ - 在正在运行的线程中处理事件

C Accept Client Socket "Invalid Argument"添加信号线程时出错

c - 在纯 C 语言中,不使用 strlen 或任何使用 strlen 的库函数,如何确定一个字符串是否包含在另一个字符串中?

c++ - 动态控制c++数据结构中的成员数量

java - OptaPlanner 具有链式变量的多个实体类

c++ - PangoLayout中忽略的新行

c++ - 如何更改 NetBeans 模板中的时间格式