[英]Bit Manipulation (extract 5th bit from X and return number of 1 bits in X)
Assume X is an unsigned integer how will I be able to extract the 5th bit from X and return the number of 1 bits in X. Note: these two operations don't depend on each other. 假设X是无符号整数,我将如何从X中提取第5位并返回X中的1位数。注意:这两个操作不相互依赖。
For extracting the 5th bit from XI was thinking to use the & operation to do something like this: XXXX1X & 000010 为了从XI中提取第5位,我想使用&operation来做这样的事情:XXXX1X&000010
I am not really sure how to do the return the number of 1 bits in X. 我不确定如何在X中返回1位的数量。
Thanks! 谢谢!
Counting the number of 1-bits is relatively straightforward; 计数1位数是相对简单的; you iterate over all of the bits of your number and add to some accumulator the number of bits that are set:
迭代你的数字的所有位并向一些累加器添加设置的位数:
unsigned int X;
unsigned int count;
for (count = 0; X; X >>= 1) {
count += X & 1;
}
How it works: 这个怎么运作:
Extracting the 5th bit is also straightforward, simply right shift your number X by 5 and compute the logical AND with 1: 提取第5位也很简单,只需将数字X右移5并用1计算逻辑AND:
unsigned int fifthBit (unsigned int X) {
return (X >> 5) & 1;
}
A shorter and simpler way to find number of set bits in an int
is following: 在
int
查找设置位数的更简单和更简单的方法如下:
int num_1_bits(unsigned int x) // or unsigned long x; or unsigned short x;
{
int bitcount = 0;
while(x)
{
x &= (x - 1);
++bitcount;
}
return bitcount;
}
How does this work? 这是如何运作的?
For an unsigned number x
, the sequence of bits in x - 1
will be the complement of x
till the first set bit of x
counted from LSB. 对于一个无符号数
x
,比特在序列x - 1
将成为补体x
,直到所述第一组位x
从LSB计数。
ie for x = 20 [0b11100]
; 即
x = 20 [0b11100]
; x - 1 = 19 [0b11011]
Here Bit sequence of 19
is complement of 20
till first set bit in 20
(first set bit at position 3 from LSB). 这里的位序列
19
是补体20
直到第一组位20
(第一组位位置3处从LSB)。
Therefore, 因此,
if you do x & (x - 1)
, this operation will consume one set bit from x
. 如果你执行
x & (x - 1)
,此操作将消耗x
一个设置位。
Hence, 因此,
if you want to continue doing this until x
becomes 0
, you will need to do it as many times as there are set bits in x
, which in turn will give you the count of set bits in x
. 如果你想继续这样做直到
x
变为0
,你需要做多次,因为x
有设置位,这反过来会给你x
中设置位的计数。
This method is better because the loop will run only as many times as there are 1
bits in x
and hence doesn't depend on datatype of x
. 这种方法更好,因为循环的运行次数与
x
中的1
位一样多,因此不依赖于x
数据类型。
作为补充,我精确,如果你正在寻找的效率,也有一些内置的硬件指令来执行对位计算(数零:你可以推断出多少个1-bit
,你有那么)看到此文章作为例子。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.