+ 3
Any one can explain this code?
#include <iostream> using namespace std; int main() { int a=3; a^=9; cout<<a<<endl; return 0; }
3 Answers
+ 10
a ^= 9 is the same as a = a ^ 9
a = 3
so
a = 3 ^ 9
^ is the bitwise (i.e. binary) XOR operator.
When one, and only one, of the expressions has a 1 in a digit, the result has a 1 in that digit. Otherwise, the result has a 0 in that digit.
3 in binary is 0011
9 in binary is 1001
--------
3 ^ 9 is 1010
1010 binary is 10 in base 10
So the output is 10.
+ 3
C++ XOR operator
http://www.cplusplus.com/doc/boolean/
+ 2
thanks @david ashton