c++ - 复制整数位的最快方法
问题描述
复制整数位的最快方法是什么。
例如,
17
->10001
复制后:
1100000011
解决方案
看起来像是比特交错的变体。
交错位是显而易见的方式
(修改自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。
推荐阅读
- confluence - 如何在 Confluence CQL 中添加条件 ORDER BY?
- reactjs - 尝试使用 cmd 为 reactjs 创建一个文件夹
- python - 将 fastAPI UploadFile 与接受类文件对象的库一起使用
- plot - plotly.go.Histogram(): 只显示水平分布线
- flutter - 如何在一定时间内执行动画
- bigcommerce - Bigcommerce 如果对 URL 的条件检查不起作用
- python - 带有外键的序列化器,如何处理前端视图和后端查询?
- c# - 请求被中止:无法为 HttpWebRequest 创建 SSL/TLS 安全通道。(从 Localhost ASPNET 4.5 工作)但从 AWS 服务器失败
- rxjs - 管道 Observable 到 Subject 不会让它不必要地变热
- python - Psycopg2/PostgreSQL 11.9:执行字符串->日期类型转换时在“::”或附近出现语法错误