简体   繁体   English

如何使用位操作找到第5位,并以整数形式返回1位的数目

[英]How to use bit manipulation to find 5th bit, and to return number of 1 bits in an integer

Assume Z is an unsigned integer. 假设Z是一个无符号整数。 Using ~, <<, >>, &, | 使用〜,<<,>>,&,| , +, and - provide statements which return the desired result. ,+和-提供返回所需结果的语句。

I am allowed to introduce new binary values if needed. 如果需要,我可以引入新的二进制值。

I have these problems: 我有这些问题:

1.Extract the 5th bit from the left Z. 1. 从左 Z提取第5位。

For this I was thinking about doing something like 为此,我正在考虑做类似的事情

  x x x x x x x x 
& 0 0 0 0 1 0 0 0 
___________________

  0 0 0 0 1 0 0 0 

Does this make sense for extracting the fifth bit? 这对于提取第五位有意义吗? I am not totally sure how I would make this work by using just Z when I do not know its values. 我不确定在不知道Z值的情况下如何仅使用Z来完成这项工作。 (I am relatively new to all of this). (我对所有这些都比较陌生)。 Would this type of idea work though? 这种想法会起作用吗?

2.Return the number of 1 bits in Z 2.返回Z中的1位数

Here I kind of have no idea how to work this out. What I really need to know is how to work on just Z with the operators, but I m not sure exactly how to. 

Like I said I am new to this, so any help is appreciated. 就像我说的那样,我是新手,所以能提供任何帮助。

Problem 1 问题1

You're right on the money. 你是对的钱。 I'd do an & and a >> so that you get either a nice 0 or 1. 我将使用&>>以便得到0或1。

result = (z & 0x08) >> 3;

However, this may not be strictly necessary. 但是,这可能不是严格必要的。 For example, if you're trying to check whether the bit is set as part of an if conditional, you can exploit C's definition of anything nonzero as true. 例如,如果您尝试检查该位是否设置为if条件的一部分, if可以利用C对非零值的定义为true。

if (z & 0x08)
        do_stuff();

Problem 2 问题2

There are a whole variety of ways to do this. 有很多种方法可以做到这一点。 According to that page, the following methodology dates from 1960, though it wasn't published in C until 1988. 根据该页面,以下方法可追溯到1960年,尽管直到1988年才在C语言中发布。

for (result = 0; z; result++)
        z &= z - 1;

Exactly why this works might not be obvious at first, but if you work through a few examples, you'll quickly see why it does. 确切的原因一开始可能并不很明显,但是如果您通过一些示例进行操作,您将很快知道它为什么起作用。

It's worth noting that this operation – determining the number of 1 bits in a number – is sufficiently important to have a name (population count or Hamming weight ) and, on recent Intel and AMD processors, a dedicated instruction . 值得注意的是,此操作(确定一个数字中的1位的数目)对于拥有名称(人口计数或汉明权重 )非常重要,并且在最近的Intel和AMD处理器上还具有专用指令 If you're using GCC, you can use the __builtin_popcount intrinsic. 如果您使用的是GCC,则可以使用__builtin_popcount内部函数。

Problem 1 looks right, except you should finish it by shifting the result right by 4 to get that bit after the mask. 问题1看起来正确,但是您应该通过将结果右移4来获得该掩码后的位来完成它。

To implement the mask, you need to know what integer is represented by a single 5th bit. 要实现掩码,您需要知道单个5位代表什么整数。 That number is incidentally 2^5 = 32. So you can just AND z with 32 and shift it right by 4. 该数字是2 ^ 5 =32。因此,您可以将z与32进行“与”运算并将其右移4。

Problem 2: 问题2:

int answer = 0;
while (z != 0){  //stop when there are no more 1 bits in z

   //the following masks the lowest bit in z and adds it into answer
   //if z ends with a 0, nothing is added, otherwise 1 is added
   answer += (z & 1);
   //this shifts z right by 1 to get the next higher bit
   z >>= 1;
}
return answer;

To find out the value of the fifth bit, you don't care about the bottom bits so you can get rid of them: 要找出第五位的值,您无需关心最低位,因此可以摆脱它们:

unsigned int answer = z >> 4;

The fifth bit becomes the bottom bit, so you can strip it off with a bitwise-AND: 第五位变为最低位,因此您可以使用按位与运算符将其剥离:

answer = answer & 1;

To find the number of 1-bits in a number you can apply stakSmashr's solution. 要查找数字中的1位数字,您可以应用stakSmashr的解决方案。 You could optimise this further if you know you need to count the number of bits in a lot of integers - precompute the number of bits in every possible 8-bit number and store it in a table. 如果您知道需要计算许多整数中的位数,则可以进一步优化此方法-预计算每个可能的8位数字中的位数,并将其存储在表中。 There will only be 256 entries in the table so it won't use much memory. 该表中只有256个条目,因此不会占用太多内存。 Then, you can loop over your data one byte at a time and find the answer from the table. 然后,您可以一次循环一个字节的数据,并从表中找到答案。 This lookup will be quicker than looping again over each bit. 这种查找比重新遍历每个位更快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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