[英]Can't understand what this C program is doing?
When I was preparing for some interview I got the following 当我准备面试时,我得到了以下内容
count=0;
for(i=1;i<=n;i++)
{
i=(i)&(i-1); //line 4
count++;
}
return count;
//--------
i=21,22 same count. For what other values of i we get same count?
I was not able to understand what line 4 is doing. 我不明白第4行在做什么。 Can anyone please help me and give me the output of the program.....
谁能帮我,给我程序的输出.....
Found the above question(9) in the link http://placement.freshersworld.com/placement-papers/Mentor-Graphics/Placement-Paper-Aptitude-General-32412 在链接http://placement.freshersworld.com/placement-papers/Mentor-Graphics/Placement-Paper-Aptitude-General-32412中找到上述问题(9)
i & (i-1) returns 0 if i is a power of 2 or if i is 0, non zero otherwise. 如果i为2的幂或i为0,则i&(i-1)返回0,否则返回非零。
On inspection, i will be set to zero, count incremented, then i will become 1 again, then 0, and so on. 在检查时,我将设置为零,计数递增,然后我将再次变为1,然后为0,依此类推。
I'm not sure that this loop will terminate . 我不确定该循环是否会终止。
Warning 警告
After looking over your code a second time, I must say, it may compile just fine, but you are going to encounter a deadlock, I do think: 寻找你的代码的第二时间之后,我必须说,这可能编译就好了,但你要遇到一个僵局,我认为:
for (i=1;i<=n;++i)
{
i = (i)&(i-1);
cont++;
}
What will happen? 会发生什么? When
i
is 1
: 当
i
是1
:
i = 1&0;//is what line 4 boils down to
So i
will be 0
, if the loop started, then n
will be at least 1, so the loop condition still is true: 因此,
i
将为0
,如果循环开始,则n
至少为1,因此循环条件仍然为true:
i<=n => 0 <= n ==> true
So i
is incremented by 1
( i++
), and the whole thing starts again. 所以
i
加1
( i++
),然后整个事情又开始了。 Line four, once again, evaluates to: 第四行再次评估为:
i = 1&0;//assigns 0 again to i
And you're back to square one. 然后您回到正题。 This program will never terminate, it'll simply repeat the same operation over and over and over...
这个程序永远不会终止,它只会一遍又一遍地重复相同的操作...
Well, &
is the bitwise AND operator. 好吧,
&
是按位AND运算符。 It When used, as in your snippet with 2 integers, it returns the bits that are "switched on" or set to 1
in both numbers. 它使用时(如您的代码段中包含2个整数),将返回“打开”或两个数字均设置为
1
位。 In plain English: the expression evaluates to a new set of bits, which were set in both operands. 用简单的英语来说:表达式的结果是一组新的位,这两个位都在两个操作数中设置。 Take, for example, when
i
is 2: 以当
i
为2时为例:
00000010 //2
00000001 // i - 1
--------
00000000
In this case, non of the bits are set to 1
in both cases. 在这种情况下,在两种情况下,没有一个比特被设置为
1
。 As indeed this will be the fact for all powers of two, because the powers of 2 look like this in binary: 确实,这是所有2的幂的事实,因为2的幂在二进制中看起来像这样:
00000010
00000100
00001000
And a power of 2 minus 1 looks like this: 2的幂减去1看起来像这样:
00001000//power of 2
00000111
In all other cases, there will, at least be 1 bit that is set to 1
in both cases: 在所有其他情况下,在两种情况下至少会有1位被设置为
1
:
00000110
00000101
--------
00000100
Easy. 简单。
For a more complete overview, and detailed explanation of bitwise operators in C, you can always refer to the wiki on bitwise operators in C 有关C中按位运算符的更完整概述和详细说明,您可以始终参考C中按位运算符的Wiki。
The line 线
i = i & (i-1)
clears the lowest set bit of i
. 清除
i
的最低设置位。 So a loop like 像这样的循环
while (i) {
i = i & (i-1);
count++; }
counts the number of set bits in i
. 计算
i
的设置位数。 Note that this really only works if i
has an unsigned type. 请注意,这仅在
i
具有无符号类型的情况下才有效。 If i
is signed and negative, it causes undefined behavior. 如果
i
签名并且为负,则将导致未定义的行为。
The link you give is probably a misremembered question (left off the while (i)
) asking about a loop like this. 您提供的链接可能是一个记错了的问题(省略了
while (i)
),正在询问这样的循环。
The comment "i=21,22 same count" is a hint that 21(10101) and 22(10110) have the same number of set bits. 注释“ i = 21,22相同计数”表示21(10101)和22(10110)具有相同数量的设置位。
I must admit that when I read your question the first time, I actually saw the while(i)
loop as the lines i=(i)&(i-1); count++;
我必须承认,当我第一次阅读您的问题时,我实际上看到
while(i)
循环为i=(i)&(i-1); count++;
i=(i)&(i-1); count++;
really only make sense in that context, and its such a common "trick question" idiom. 实际上只有在这种情况下才有意义,而且它是一个如此常见的“技巧问题”惯用法。
& is a bit-wise AND operator.It computes as follows: &是按位与运算符,其计算如下:
for eg: i=8 例如:i = 8
i & (i-1) is (1000) AND (0111) results 0000.
if you run the program it gets into infinite loop since 如果您运行该程序,它将进入无限循环,因为
i=1 and (i) & (i-1) gives 0
every time i becomes 1 每当我变成1
so, modify the code as follows and I hope you will get the answer: 因此,如下修改代码,希望您能得到答案:
for(i=1;i<=n;i++)
{
count=(i)&(i-1);
printf(" i= %d count= %d",i,count);
}
The result is as follows: 结果如下:
i= 1 count= 0
i= 2 count= 0
i= 3 count= 2
i= 4 count= 0
i= 5 count= 4
i= 6 count= 4
i= 7 count= 6
i= 8 count= 0
i= 9 count= 8
i= 10 count= 8
i= 11 count= 10
i= 12 count= 8
i= 13 count= 12
i= 14 count= 12
i= 15 count= 14
i= 16 count= 0
i= 17 count= 16
i= 18 count= 16
i= 19 count= 18
i= 20 count= 16
i= 21 count= 20
i= 22 count= 20
i= 23 count= 22
i= 24 count= 16
i= 25 count= 24
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.