What will be the value of x after executing the following C++ code?

int x=4;
x |= 3;
cout << x;
                        

Options

  • A. 3
  • B. 4
  • C. 7
  • D. More than one of the above
  • E. None of the above

Correct Answer (Detailed Explanation is Below)

C. 7

Detailed Explanation

Answer: (C) 7

The operator |= is a compound assignment operator. It performs a bitwise OR and then assigns the result back to the variable.

Therefore:

x |= 3 is equivalent to x = x | 3.

Now:

4 = 100
3 = 011

100 | 011 = 111

111 is 7 in decimal.