Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

jump
conditional jump
conditional

What is the shift instruction set?

Arslan Tariq

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code

Introduction

The shift instruction set is one of the most helpful instruction sets in the processor. They simplify very complex tasks using very concise algorithms. Our processors provide the following shift operations:

  • Shift logical right (SHR)
  • Shift logical left (SHL)
  • Shift arithmetic right (SAR)

Shift logical right (SHR)

A logical right shift operation inserts a zero from the left, shifts each bit one position to the right, and copies the rightmost bit into the carry flag. Imagine a tube full of eight balls. The tube is open on both ends, and the right end has a basket for anything that falls from there. The action of a logical right shift is to force a white ball from the left end. The operation is shown in the figure below. Zero represents a white ball, while one represents a black ball. 16 bits are toggled in the same way using a dual capacitor pipe.

Shift logical right

Shift logical left (SHL) / Shift arithmetic left (SAL)

A logical left-shift operation is the opposite of a logical right shift. In this operation, vacancies are inserted from the right, and each bit is shifted from one position to the left, with the most significant bit falling into the carry flag. Left shift arithmetic is just another name for a logical left shift. The operation is illustrated again using the following image of the ball and pipe.

Shift logical left

Shift arithmetic right (SAR)

Signed numbers keep the sign in their most significant bit. If the bit is 1, the correct logical shift will change the sign of the number by inserting zeros from the left. The sign of a signed number must not be changed by shifting.

So, a correct shift arithmetic operation is to shift each bit from one position to the right and copy the most significant bit at the most significant bit. The bits that fell from the right are collected in the basket. The sign bit is preserved in this operation. This operation is further described below.

Shift arithmetic right

Move left is multiplied by 2, and move right is divided by 2. However, instead of sliding logically, We can use arithmetic operations to divide a number by 2 to move it to the right. A left shift operation is equivalent to multiplication unless the left key bit is omitted. If this happens, the overflow flag will signal this and can be checked with JO (Jump Overflow).

For signed numbers divided by 2, moving to the right logically will give the wrong answer for negative numbers because zeros inserted from the left change the sign. Shift arithmetic correct statement must be used on the drawn numbers to maintain the draw flag and effectively divide by two.

RELATED TAGS

jump
conditional jump
conditional

CONTRIBUTOR

Arslan Tariq
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code
Keep Exploring