Introduction to Logical Shift And Its Use Cases

If like me, you have a degree in computer science, you must have encountered logical shifts during the course of your academic studies, if not, if you have learnt pretty much any programming language, you would have encountered them. The issue is that most people treat them as something to be aware of but not really understood. If you give me a couple of minutes, I hope to help simplify it to you.

What is logical shift

Logical shift is a type of bitwise operation that shifts/moves the bits in its operands in a particular direction. In simple terms, if you logically shift X by Y, it means that the operation will move the bits in X by Y positions. Still sounds geeky, let us break it down together.

There are two types of logical shifts, namely

  1. Right Shift (denoted by >> )
  2. Left Shift (denoted by << )

If you consider the definition above, Right shift means that the bits in (X) would be shifted to the right by Y places, also Left shift means that the bits in (X) would be shifted to the left by Y places.

Example of Left Shift

let X = 4 and Y = 2, lets find X << Y

Convert X  to binary, then you have

X = 100  (i.e. the binary representation of 4 is 0000 0100 )

Note: you should use the 8 digits representation

If you find the binary calculation confusing, please read up about it here.

Now, to calculate (X << Y), i.e we move the bits in X  to the left by 2 places.

  1. before move X = 0000 0100
  2. after left move (1) , X = 0000 1000
  3. after leftmove (2) , X = 0001 0000

Now we convert (0001 0000) back to decimal and we have 2.

Hence, 4 << 2  == 16.

Example of Right Shift

let X = 8 and Y = 2, lets find X >> Y

Convert X  to binary, then you have

X = 1000  (i.e. the binary representation of 8 is 0000 1000).

Now, to calculate (X >> Y), i.e we move the bits in X to the right by 2 places.

  1. before move X = 0000 1000
  2. after right move (1) , X = 0000 0100
  3. after right move (2) , X = 0000 0010

Note: since the first 4 digits in the binary value of X is (0) and we are shifting to the right, we could have discared it and just used (1000).

Now we convert (0010) back to decimal and we have 2.

Hence, 8  >> 2  is equal to 2.

Use cases of logical shifts (both left and right).

Left shift (<<) is used to multiply a value by any power of 2 and Right bit shift (>>) is used to divide by any power of two. consider the table below

Cheatsheet or Ways of calulating the logical shifts faster

The left logical shift (<<) is the same as

Left Bit Shift
# Operation X Y Resolution Result
1 X << Y 8 2 8 * pow(2,2) 32
2 X << Y 2 4 2 * pow(2,4) 32
3 X << Y 3 2 3 * pow(2,2) 12

and,

Right Bit Shift
# Operation X Y Resolution Result
1 X >> Y 64 4 64 / pow(2,4) 4
2 X >> Y 4 2 4 / pow(2,2) 1
3 X >> Y 8 2 8 / pow(2,2) 2

So, that is all. Hope i have made the use of logical shift alot clearer to you.

giphy1

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s