Tag: 位标志

确定字节中的哪个位被设置

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