What will be the output of the following C++ code?

int x=8;
if(x & 8)
    cout << "Yes";
else
    cout << "No";
                        

Options

  • A. Yes
  • B. No
  • C. 8
  • D. More than one of the above
  • E. None of the above

Correct Answer (Detailed Explanation is Below)

A. Yes

Detailed Explanation

Answer: (A) Yes

The condition uses the bitwise AND operator:

x & 8

Here x = 8.

In binary:

8 = 1000

Therefore:

1000 & 1000 = 1000

The result is 8, which is a non-zero value. A non-zero value is treated as true in a conditional expression.

Therefore, Yes is printed.