[英]bitwise operators with an if
I am reading K&R book for c language and in section 2.10 they give the following example:我正在阅读 c 语言的 K&R 书,在第 2.10 节中,他们给出了以下示例:
/*bitcount: count 1 bits in x*/
int bitcount(unsigned x)
{
int b;
for(b=0; x!=0;x>>=1)
if(x&01)
b++;
return b;
}
The function supposed to count the bits that are 1 in x. function 应该计算 x 中为 1 的位。
I understand that the if is supposed to "mask off" the bits, but I don't understand how?我知道 if 应该“屏蔽”这些位,但我不明白怎么做?
Is This condition is basicly:这个条件基本上是:
if(x&01==1)?
I don't understand this condition.我不明白这个条件。
What does (x&01) mean? (x&01) 是什么意思?
Also, I don't understand when does the loop stop?另外,我不明白循环什么时候停止? whenever all the bits have been shifted to right and all the vacated cells are now 0?
每当所有位都向右移动并且所有空出的单元格现在都为 0 时?
I just can't understand how this method works, and I looked for a solution quite a while.我只是不明白这种方法是如何工作的,我找了一段时间的解决方案。
Thank you.谢谢你。
Let's rewrite the function using a while loop. 让我们使用while循环重写函数。
int bitcount(unsigned x)
{
int b = 0;
while (x != 0) {
if (x & 0x1)
b++;
x = x >> 1;
}
return b;
}
Note that each iteration of the loop, we do two things: 注意循环的每次迭代,我们做两件事:
(x = x >> 1)
. (x = x >> 1)
。 Is This condition is basicly: if(x&01==1)?
这个条件是基本的:if(x&01 == 1)?
Kind of, the condition in other words: if the "x & 01" is non-zero. 换句话说,条件是:如果“x&01”非零。
Also, I don't understand when does the loop stop?
另外,我不明白循环什么时候停止? whenever all the bits have been shifted to right and all the vacated cells are now 0?
每当所有位都向右移动并且所有空出的单元现在都是0时?
When you do x>>=1, you are shifting all the bits x to the right by 1 step . 当你执行x >> = 1时,你将所有位x向右移动1步 。 If you take a pen and paper, you will also realize that this is same as dividing by 2. When will it stop?
如果你拿笔和纸,你也会发现这与除以2相同。什么时候会停止? When this
x
becomes zero: x!=0
. 当此
x
变为零时: x!=0
。
You understood right about loop termination. 你对循环终止有所了解。
But however an important note : This code will work only if x
is unsigned
. 但是一个重要的注意事项 :只有当
x
是unsigned
此代码才有效。 Because for a signed integer 1
bit is appended as MSB on right shift. 因为对于有符号整数,
1
位作为右移的MSB附加。
Now (x & 01 == 1)
: 现在
(x & 01 == 1)
:
It basically ANDs the value of x
to decimal number 1
like this: 它基本上将
x
的值与十进制数1
AND运算,如下所示:
x: 0001000100001110 (Some random 32 bit value) x:0001000100001110(一些随机32位值)
1: 0000000000000001 1:0000000000000001
& &
Result: 0000000000000000 结果:0000000000000000
Reason for this result: 这个结果的原因:
AND operation does a bit by bit logical AND. AND操作逐位逻辑AND。 You can check the truth table for AND operation on internet.
您可以在互联网上查看AND操作的真值表。
A tip/hint for you : This is not the best method to count all the 1
bits in a signed/unsigned number. 给你的提示/提示 :这不是计算有符号/无符号数中所有
1
位的最佳方法。 There exists a method which can count 1
bits in as many loop iterations as there are number of 1
s in the number. 存在其中可以指望的方法
1
因为有数量在尽可能多的循环迭代的比特1
中的数s。 Try yourself to implement it or search for that better method. 尝试自己实现它或搜索更好的方法。
if(x&01)
means: if(x&01)
意味着:
for example1: x's binary value is: 1011101例如1:x的二进制值为:1011101
01's binary value is: 0000001 01的二进制值为:0000001
& is binaryopAND: --------- & 是二进制opAND:--------
result is: 0000001结果是:0000001
for example2: x's binary value is: 1011100 (last bit is 0)例如2:x的二进制值为:1011100(最后一位为0)
01's binary value is: 0000001 01的二进制值为:0000001
& is binaryopAND: --------- & 是二进制opAND:--------
result is: 0000000结果是:0000000
so this means if(x&01)
:所以这意味着
if(x&01)
:
this condition returns only 2 values "0000001" or "0000000" according to x's last bit value "1" or "0".根据 x 的最后一位值“1”或“0”,此条件仅返回 2 个值“0000001”或“0000000”。
int bitcount(unsigned x)
: int bitcount(unsigned x)
:
x is defined as unsigned x 定义为无符号
this means x can be only a positive value.这意味着 x 只能是正值。
cuz ungsigned means only positive numbers.因为无符号意味着只有正数。
0000000 is NOT POSITIVE NUMBER = NOT UNSINGED NUMBER. 0000000 不是正数 = 不是未签名的数字。
so b++;
所以
b++;
: :
since "x" is defined in only postive numbers, x won't accept 0000000 value cuz 0000000 is a not postive number.由于“x”仅以正数定义,因此 x 不会接受 0000000 值,因为 0000000 不是正数。 This will only count values are 0000001.
这只会计数值为 0000001。
finally x >>= 1
:最后
x >>= 1
:
this will shift x's bits 1 slot to the right-side and will fill the new slot bit that created at the most-left with "0" value.这会将 x 的位向右移动 1 个槽,并将用“0”值填充在最左侧创建的新槽位。
and x != 0
:和
x != 0
:
"for" loop will end when x comes to 0000000 in the end of process of right-side shifting eventually.最终右移过程结束时,当 x 到达 0000000 时,“for”循环结束。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.