What is the output of the following C++ code?

int x=7;
int y=5;
cout << (x ^ y);
                        

Options

  • A. 2
  • B. 3
  • C. 1
  • D. More than one of the above
  • E. None of the above

Correct Answer (Detailed Explanation is Below)

A. 2

Detailed Explanation

Answer: (A) 2

The ^ operator performs a bitwise XOR operation.

7 = 0111
5 = 0101

In XOR, the result is 1 when the two corresponding bits are different.

0111
0101
----
0010

0010 is 2 in decimal.