0

Right Shift and Left Shift operator moves all bits right and left respectively based on numbers of bits we have specified in operation.

Example:

int x = 1 and we do x = x >> 1, it moves all bits right one position and places one extra bit at left as 0.

But in case of negative numbers say x = -1. Its binary equivalent(in integer - 32 bits) is 11111111111111111111111111111111 (32 bits)

If we move one bit right x = x >> 1 zero is not being added at left side and after operation it is still 11111111111111111111111111111111(32 bits).

But if we move it one bit left x = x << 1 zero is being added at right side and it becomes 11111111111111111111111111111110(32 bits).

Same is not true with positive numbers.

i.e. when we right shift positive number one bit zero is being added at left side whereas in case of negative numbers 1 is being added.

Ryan Leach
  • 4,262
  • 5
  • 34
  • 71
Rohit Shekhar
  • 315
  • 4
  • 15

0 Answers0