+ 3

Any one can explain this code?

#include <iostream> using namespace std; int main() { int a=3; a^=9; cout<<a<<endl; return 0; }

28th Sep 2017, 6:27 AM
vishal ranjan
vishal ranjan - avatar
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.
28th Sep 2017, 7:13 AM
David Ashton
David Ashton - avatar
28th Sep 2017, 6:39 AM
Fermi
Fermi - avatar
+ 2
thanks @david ashton
28th Sep 2017, 7:25 AM
vishal ranjan
vishal ranjan - avatar