# What is left rotation (<<<) ??

I was solving problem One Dimensional Game of Life and came across left and right rotation in its editorial.
Any help or suggestions would be appreciated . Thanks

NUmbers are stored internally in binary. 2 is 10, 3 is 11, 4 100 and so on. Left shifting shifts the digit in this representation by 1. eg: 0010 left shifted is 0100. 0101 left shifted is 1010. Right shift is the opposite of left shift and shifts digit to right. 0010 right shifted is 0001 and 0101 right shifted is 0010.

Left shift is equivalent to multiplying a number by 2 and right shift is equivalent to dividing by 2(and leaving remainder). 4 << 1 is left shifting 4 once. 4<<1 = 8, 4<2=16. 4>>1= 2 , 5>>1 = 2.

In left shifting of digits we add a zero towards the end. In left rotation, we move the right most bit to the right and vice versa for right rotation.The examples given below by @achaitanyasai

The main advantage of digit rotation is that it is faster than normal division and also helps when operations are to be done on individual bits

2 Likes

Correct me If I’m wrong… left rotation is same as left shift and right rotation is same as right shift…agree…??

1 Like

No they are not. let us take this example : 1001 and perform all the four operations :

Left shift -> 0010

Right shift -> 0100

Left Rotation -> 0011

Right Rotation -> 1100

3 Likes

@achaitanyasai thanks man…I got it.

Thanks for correcting. I answered for left shift and right shift. Will extend it to cover rotation

consider the expression a>>1(arithmatic) and a>>>1(logical)

if a is a positive number both these expressions will give same results, but

if a is a negetive number then results are different

example:
consider

a=2 00000010

b=-2 11111110 (in 2s complement form ie. take 1s complement of a and add 1 so 11111101+00000001=11111110)

now

b>>1 11111111 ie right shift is done but the sign bit is preserved

b>>>1 01111111 ie. blindly right shift is done without checking the sign bit

the >>> is not available in C++/C but is available in JAVA

there is no “<<<” as logical and arithmetic left shifts are same

1 Like

thanks @coda

no problem

//