What will be the output of the following C++ code?
int x=3;
cout << (x << 2);
Options
- A. 6
- B. 12
- C. 9
- D. More than one of the above
- E. None of the above
Quiz Practice:
int x=3;
cout << (x << 2);
B. 12
Answer: (B) 12
The << operator performs a left shift.
The value 3 in binary is 0011.
Shifting it left by 2 positions gives:
0011 << 2 = 1100
1100 in decimal is 12.
For positive integer values, a left shift by one position is commonly equivalent to multiplying by 2, so shifting by two positions gives 3 × 4 = 12.