How does condition statement work with bit-wise operators?

我怕爱的太早我们不能终老 提交于 2020-01-02 04:01:07

问题


I tried to understand how if condition work with bitwise operators. A way to check if a number is even or odd can be done by:

#include <iostream>
#include <string>
using namespace std;

string test()
{
    int i = 8;  //a number
    if(i & 1)
      return "odd";

    else
      return "even";       
}

int main ()
{
  cout << test();
  return 0;
}

The Part I don't understand is how the if condition work. In this case if i = 8 then the in If statement it is doing 1000 & 1 which should gives back 1000 which equal 8.

If i = 7, then in if statement it should be doing 111 & 1 which gives back 111 which equal 7

Why is it the case that if(8) will return "even" and if(7) return "odd"? I guess I want to understand what the if statement is checking to be True and what to be False when dealing with bit-wise operators.

Just A thought when I wrote this question down is it because it's actually doing

for 8: 1000 & 0001 which gives 0
for 7: 0111 & 0001 which gives 1?

回答1:


Yes, you are right in the last part. Binary & and | are performed bit by bit. Since

1 & 1 == 1
1 & 0 == 0
0 & 1 == 0
0 & 0 == 0

we can see that:

8 & 1 == 1000 & 0001 == 0000

and

7 & 1 == 0111 & 0001 == 0001

Your test function does correctly compute whether a number is even or odd though, because a & 1 tests whether there is a 1 in the 1s place, which there only is for odd numbers.




回答2:


Actually, in C, C++ and other major programming languages the & operator do AND operations in each bit for integral types. The nth bit in a bitwise AND is equal to 1 if and only if the nth bit of both operands are equal to 1.

For example:

8 & 1 =
1000 - 8
0001 - 1
----
0000 - 0

7 & 1 =
0111 - 7
0001 - 1
----
0001 - 1

7 & 5 =
0111 - 7
0101 - 5
----
0101 - 5

For this reason doing a bitwise AND between an even number and 1 will always be equal 0 because only odd numbers have their least significant bit equal to 1.




回答3:


if(x) in C++ converts x to boolean. An integer is considered true iff it is nonzero.

Thus, all if(i & 1) is doing is checking to see if the least-significant bit is set in i. If it is set, i&1 will be nonzero; if it is not set, i&1 will be zero.

The least significant bit is set in an integer iff that integer is odd, so thus i&1 is nonzero iff i is odd.




回答4:


The expression i & 1, where i is an int, has type int. Its value is 1 or 0, depending on the value of the low bit of i. In the statement if(i & 1), the result of that expression is converted to bool, following the usual rule for integer types: 0 becomes false and non-zero becomes true.




回答5:


What you say the code is doing is actually how bit-wise operators are supposed to work. In your example of (8 & 1):

1000 & 0001 = 0000

because in the first value, the last bit is set to 0, while in the second value, the last bit is set to 1. 0 & 1 = 0.

0111 & 0001 = 0001

In both values, the last bit is set to 1, so the result is 1 since 1 & 1 = 1.



来源:https://stackoverflow.com/questions/13054401/how-does-condition-statement-work-with-bit-wise-operators

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!