简体   繁体   English

位操作(从X中提取第5位并在X中返回1位数)

[英]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: 这个怎么运作:

  1. Initialize count to 0 将计数初始化为0
  2. Start from the MSB (most significant bit) [this is the current bit] 从MSB开始(最高位)[这是当前位]
  3. Add to count the result of the current bit & 1 添加以计算当前位&1的结果
    • 1 if the bit is set 如果该位置位,则为1
    • 0 if the bit is not set 如果未设置该位,则为0
  4. Shift X to the right 1 bit, so the current bit is now the next MSB 将X向右移1位,因此当前位现在是下一个MSB
  5. Repeat step 3 重复步骤3

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM