+ 3
Hey guys. Can some one explain how this code works? I don't know what does this single & operator do.
int x=15; int y=7; if((++x<=15) & (y++==7)){ Console.Write(x); } else{ Console.Write(y); }
2 odpowiedzi
+ 10
Single & operator is known as bitwise AND. It performs AND logic bit by bit. For example:
6 & 3
= 110 & 011 (binary equivalence of 6 and 3)
= (1&0)(1&1)(0&1)
= 010
= 2
For the given program, the operands have only one bit each.
(++x <= 15) & (y++ == 7)
= (16 <= 15) & (7 == 7) // y is 8 but y++ is 7
= 0 & 1
= 0
So, if condition is FALSE, and thus the else body is executed which prints 8 (current value of y)
+ 9
&& is logical operator
and
& is binary operator(and gate)
at your code
(16<=15)&(7==7)
false & true
false
then print y value out
understand?