Why is 2 + 1 & 0 the result is 0?

In VC2008, I typed this code:

int a = 2 + 1 & 0; 

and this expression is obtained a = 0

Why is the result equal to 0 but not 2?

+6
source share
2 answers

Since the operator & is evaluated after the operator + and 3 & 0 is 0 .

Of course, you can put braces around expressions to change the order of evaluation. For instance:

 int a = 2 + (1 & 0); /* a == 2 */ 
+17
source

+ has a higher priority than & . Here is a complete table of operator priorities.

+5
source

Source: https://habr.com/ru/post/904679/


All Articles