📅  最后修改于: 2023-12-03 15:39:56.437000             🧑  作者: Mango
As a programmer, it is important to constantly update our knowledge and skills. One great resource for doing so is by practicing for competitive exams like UGC-NET CS. In this article, we will discuss the problem 22 from the 2017 December 2nd exam and provide a solution for it.
Consider the following program:
int main()
{
int i, x = 32;
for (i = 0; i < 4; i++)
{
x = x << i;
cout << x << endl;
}
return 0;
}
What will be the output of the above program?
The above program is using bitwise left shift operator (<<
) to shift the value of x
to the left by i
bits in each iteration of the loop. We can see that the value of x
is initialized to 32
which is 0b100000
in binary.
In the first iteration, x
is shifted left by 0
bits, which means it stays the same. Hence the output on the first line will be 32
.
In the second iteration, x
is shifted left by 1
bit, which will result in 64
(in binary 0b1000000
).
In the third iteration, x
is shifted left by 2
bits, which will result in 128
(in binary 0b10000000
).
In the fourth and final iteration, x
is shifted left by 3
bits, which will result in 256
(in binary 0b100000000
).
Therefore, the output of the program will be:
32
64
128
256
By solving competitive programming problems like these, programmers can improve their problem-solving skills and gain a deeper understanding of programming concepts. We hope this article provided some valuable insights into the bitwise left shift operator and how it can be used in programming.