Shift, how does it work?


I have been asked by millions(exaggerating) as to how ‘Shifts’ work ,Meaning the ‘Right shift’ and/or ‘Left shift’

Syntax

operand << / >> ‘shift no’

Right shift, >>

The right most bit will be lost and a 0 is attached from the left side ,ie at the MSB position.

Thumb-rule: Easiest way to find out is to divide the operand by 2^’shift no’ and the result will give you the final value, then take its binary rep

Eg, 10>>2

10 /( 2^2)  = 2

Left shift, <<

When you left shift an operand, the left most bit is lost, and 0 is attached from the right side.

Thumb rule:Easiest way to find out is to multiply the operand by 2^’shift no’ and the result will give you the final value, then take its binary rep

Eg,

10<<2

10 * (2^2) = 40

Thats it!!!!

Cheers 🙂

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s