我有一个byte我用于比特标志.我知道在任何给定时间都设置了一个且只有一个位byte.
例如: unsigned char b = 0x20; //(00100000) 6th most bit set
我目前使用以下循环来确定设置了哪个位:
int getSetBitLocation(unsigned char b) {
int i=0;
while( !((b >> i++) & 0x01) ) { ; }
return i;
}
Run Code Online (Sandbox Code Playgroud)
如何最有效地确定设定位的位置?我可以不经迭代地完成这项工作吗?