Count the consecutive zero bits (trailing) on the right with multiply and lookup
我在网上看到了一点神奇的代码,用来计算一个数字末尾连续零的个数。
刚好我在优化一个I2C读写函数(只写入I2C特定bit),觉得这个很有用。经过尝试,确实没问题。
下面我隆重介绍一下:
Count the consecutive zero bits (trailing) on the right with multiply and lookup
unsigned int v; // find the number of trailing zeros in 32-bit v
int r; // result goes here
static const int MultiplyDeBruijnBitPosition[32] =
{
0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
};
r = MultiplyDeBruijnBitPosition[((uint32_t)((v & -v) * 0x077CB531U)) >> 27];
Converting bit vectors to indices of set bits is an example use for this. It requires one more operation than the earlier one involving modulus division, but the multiply may be faster. The expression (v & -v) extracts the least significant 1 bit from v. The constant 0x077CB531UL is a de Bruijn sequence, which produces a unique pattern of bits into the high 5 bits for each possible bit position that it is multiplied against. When there are no bits set, it returns 0. More information can be found by reading the paper Using de Bruijn Sequences to Index 1 in a Computer Word by Charles E. Leiserson, Harald Prokof, and Keith H. Randall.
On October 8, 2005 Andrew Shapira suggested I add this. Dustin Spicuzza asked me on April 14, 2009 to cast the result of the multiply to a 32-bit type so it would work when compiled with 64-bit ints.
以上内容转自http://graphics.stanford.edu/~seander/bithacks.html
++++++++++++++++++++++++++++++++我是分割线++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
然后根据此方案,我们来做一个8bit的末尾连续0的计算。
主要用途是I2C读写时,mask 的计算。如果要读写某Byte的某几个bit,可以只传入寄存器地址和Mask位就可以了。
创建文件test.c
#include <stdio.h>
#include <stdlib.h> void main(int argc, **argv)
{
int r;
unsigned char v;
static const unsigned char MultiplyDeBruijnBitPosition[] = {, , , , , , , }; v = (unsigned char)stroul(argv[],NULL, );
r = MultiplyDeBruijnBitPosition[((unsigned char)((v & -v) * 0x1DU)) >> ]; printf("The calculated = %d\n", r);
}
在Linux下调试验证:
gcc -o test test.c
./test xxx
应用实例:
mask的计算方法
#define BIT(nr) (1<<(nr))
#define _PM_MASK(BITS, POS) \
((unsigned char)((( << (BITS)) - ) << (POS)))
#define PM_MASK(LEFT, RIGHT) \
_PM_MASK((LEFT) - (RIGHT) + , RIGHT)
下面是带mask的I2C写操作。
static status_t i2c_write_mask(uint8_t reg, uint8_t mask, uint8_t data)
{
status_t status;
uint8_t shift = ;
uint8_t tmp; status = i2c_read_byte(reg, &tmp);
if (status != kStatus_Success) {
PRINTF("Failed: status=%ld, reg=%d\n", status, reg);
goto out;
} tmp &= ~mask;
/* 0x1D is calculated from the de Bruun sequence */
shift = MultiplyDeBruijnBitPosition[((uint8_t)((mask & -mask) * 0x1DU)) >> ];
data <<= shift;
tmp |= data & mask; status = i2c_write_byte(reg, tmp);
if (status != kStatus_Success) {
PRINTF("Failed: reg=%02X, status=%ld\n", reg, status);
} out:
return status;
}
至于像 0x1DU 及类似的代码中的 0x077CB531,0x5F3759DF和数组
{
0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
};
{0, 1, 6, 2, 7, 5, 4, 3};
的由来问题,就参考:http://www.cnblogs.com/shangdawei/p/3967505.html 好了,非常感谢此作者的工作,让我很快明白了De Bruijin序列的计算方法。
Count the consecutive zero bits (trailing) on the right with multiply and lookup的更多相关文章
- BitHacks
备份文件时看到的.我以前居然下过这东西. 2016-12-4 12:05:52更新 纯文本格式真棒.假如使用word写的我能拷过来格式还不乱?? Markdown真好. Bit Hacks By Se ...
- Bit Twiddling Hacks
http://graphics.stanford.edu/~seander/bithacks.html Bit Twiddling Hacks By Sean Eron Andersonseander ...
- upc组队赛17 Bits Reverse【暴力枚举】
Bits Reverse 题目链接 题目描述 Now given two integers x and y, you can reverse every consecutive three bits ...
- CCPC2018 桂林 D "Bits Reverse"
传送门 题目描述 Now given two integers x and y, you can reverse every consecutive three bits ,,) means chan ...
- RFID 基础/分类/编码/调制/传输
不同频段的RFID产品会有不同的特性,本文详细介绍了无源的感应器在不同工作频率产品的特性以及主要的应用. 目前定义RFID产品的工作频率有低频.高频和甚高频的频率范围内的符合不同标准的不同的产品,而且 ...
- 二维码详解(QR Code)
作者:王子旭链接:https://zhuanlan.zhihu.com/p/21463650来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 2016.7.5 更新:长文 ...
- LUXURY15
A - Guess Your Way Out! Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- RFID Exploration and Spoofer a bipolar transistor, a pair of FETs, and a rectifying full-bridge followed by a loading FET
RFID Exploration Louis Yi, Mary Ruthven, Kevin O'Toole, & Jay Patterson What did you do? We made ...
- 算术编码JM实现
h.264标准中,CABAC的算术编码部分(9.3.4)只是一个参考,实际编码器中并不一定会按照它来实现,像JM中就有自己的算术编码实现方案. 在上篇文章CABAC中有详细的算术编码描述,在了解算术编 ...
随机推荐
- 最接近的点配对(分治问题理解) && HDU 1007代码
题目大意: 给定一堆点集,在这一堆点集中找到一组点集它们之间的距离达到最短 对于HDU1007因为求圆的半径,所以距离还要除以2 普通情况下,可以将nge点,将任意两个点之间的距离都算一遍,在循环过程 ...
- noip模拟赛 蒜头君的坐骑
分析:标准的棋盘dp问题. 如果没有技能,那么就很好做了,相当于传纸条的做法.有了技能的限制,我们就要加上一维表示用了多少次技能,这个时候转移就要用到dfs了,而且不能用填表法,要用刷表法,从当前位置 ...
- HBase连接数据库(集群)
一.使用java接口对hbase进行表的创建1.引入需要的jar包2.代码: public static void main(String[] args) throws Exception { //得 ...
- Hihocoder 1337 (splay)
Problem 平衡树 SBT 题目大意 维护一个序列,支持两种操作. 操作一:插入一个数. 操作二:询问第k小的数. 解题分析 ~~刷刷水题,再熟悉一下splay的基本操作. ps:哇咔咔,有连续四 ...
- Spring Boot在开发时实现热部署(开发时修改文件保存后自动重启应用)(spring-boot-devtools)
热部署是什么 大家都知道在项目开发过程中,常常会改动页面数据或者修改数据结构,为了显示改动效果,往往需要重启应用查看改变效果,其实就是重新编译生成了新的Class文件,这个文件里记录着和代码等对应的各 ...
- Linux查看设备信息命令
系统 #查看内核/操作系统/CPU信息 uname -a #查看操作系统版本 head -n 1 /etc/issue #查看CPU信息 cat /proc/cpuinfo #查看计算机名 hostn ...
- Windows 由于无法验证发布者,windows阻止控件安装怎么办
1 打开Internet选项 2 下载未签名的ACTIVEX控件-设为启动
- Java---15---单例设计模式:---饿汉式和懒汉式
概念: 单例模式确保某个类仅仅有一个实例.并且自行实例化并向整个系统提供这个实例. 单例模式有下面特点: 1.单例类仅仅能有一个实例 2.单例类必须自己给自己创建自己的唯一实例 3.单例类必须给全 ...
- Android 录制屏幕的实现方法
Android 录制屏幕的实现方法 Chrome 2017-02-15 15:32:01 发布 您的评价: 5.0 收藏 0收藏 长久以来,我一直希望能够直接从Androi ...
- ios 使用第三方框架注意
在ios中使用第三方类库 在项目开发中经常会用到一些第三方类库,通常有两种方法来做到:一种方法是直接把所有的.h和.m文件复制到项目中:另一种方法是把.xcodeproj拖到项目中生成静 ...