i didn’t how we got the ans i.e. i understood how to write code but didn’t understand how we got the output please tell briefly?
Shift operators
hi @sarthaks03
right shift operator, x >> 1, will basically shift all the bits in the binary representation by 1 to the right, and left shift operator, x << 1, will shift the bits 1 to the left
so for eg if x=5
then binary representation is : 101
if we do x << 2 then all bits should be shifted left by 2 places, making the number 10100 and when we translate it to decimal, it becomes 2^2 + 2^4 = 4+16 = 20
similarly for y = 7, its 111 in binary form
y >> 1 will shift all bits to right by 1, making the number 11 in binary, which is 3 in decimal form.
As a shortcut, you can remember that left shift operator, x << y will do x * 2^y
and the right shift operator, x >> y, will do x / 2^y
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.