## Introduction

As you know, a computer works on instructions. Our systems consist of machine instructions to perform some operations like add, subtract, multiply, divide etc. To perform these operations, the system stores the data in registers. Now, to operate this data, the CPU has micro-operations. A **micro-operation **is a simple operation performed on the data stored in one or more registers. They transfer the data between registers. There are four types of micro-operations:-

- Register micro-operations
- Arithmetic micro-operations
- Logic micro-operations
- Shift micro-operations

In this blog, we will discuss shift micro-operations.

Recommended Topic, __Microinstruction in Computer Architecture__

## What are Shift Micro-Operations in Computer Architecture?

Shift micro-operations are used when the data is stored in registers. These micro-operations are used for the serial transmission of data. Here, the data bits are shifted from left to right. These micro-operations are also combined with arithmetic and logic micro-operations and data-processing operations.

There are three types of shift micro-operations-

- Logical Shift
- Arithmetic Shift
- Circular Shift

Letâ€™s start with logical shift micro-operation.

### Logical Shift

The logical shift micro-operation moves the 0 through the serial input. There are two ways to implement the logical shift.

- Logical Shift Left
- Logical Shift Right

Letâ€™s discuss both of them one by one.

#### Logical Shift Left

Each bit in the register is shifted to the left one by one in this shift micro-operation. The most significant bit (MSB) is moved outside the register, and the place of the least significant bit (LSB) is filled with 0.

For example, in the below data, there are 8 bits 00001010. When we perform a logical shift left on these bits, all these bits will be shifted towards the left. The MSB or the leftmost bit i.e. 0 will be moved outside, and at the rightmost place or LSB, 0 will be inserted as shown below.

To implement the logical shift left micro-operation, we use the **shl **symbol.

For example, R1 -> shl R1.

This command means the 8 bits present in the R1 register will be logically shifted left, and the result will be stored in register R1.

Moreover, the logical shift left microoperation denotes the multiplication of 2. The example weâ€™ve taken above when converted into decimal forms the number 10. And the result after the logical shift operation when converted to decimal forms the number 20.

Next, we will see the logical shift right micro-operation.

#### Logical Shift Right

Each bit in the register is shifted to the right one by one in this shift micro-operation. The least significant bit (LSB) is moved outside the register, and the place of the most significant bit (MSB) is filled with 0.

For example, in the below data, there are 8 bits 00000101. When we perform a logical shift right on these bits, all these bits will be shifted towards the right. The LSB or the rightmost bit i.e. 1 will be moved outside, and at the leftmost place or MSB, 0 will be inserted as shown below.

To implement the logical shift right micro-operation, we use the **shr **symbol.

For example, R1 -> shr R1.

This command means the 8 bits present in the R1 register will be logically shifted right, and the result will be stored in register R1.

Logical right shift micro-operation generally denotes division by 2. The inputted bits when converted into decimal form the number 5. And the outcome when converted into decimal forms the number 2.

Next, we will study arithmetic shift micro-operation.

### Arithmetic Shift

The arithmetic shift micro-operation moves the signed binary number either to the left or to the right position. There are two ways to implement the arithmetic shift.

- Arithmetic Shift Left
- Arithmetic Shift Right

Letâ€™s discuss both of them one by one.

#### Arithmetic Shift Left

The arithmetic shift left micro-operation is the same as the logical shift left micro-operation. Each bit in the register is shifted to the left one by one in this shift micro-operation. The most significant bit (MSB) is moved outside the register, and the place of the least significant bit (LSB) is filled with 0.

For example, in the below data, there are 8 bits 00100011. When we perform the arithmetic shift left on these bits, all these bits will be shifted towards the left. The MSB or the leftmost bit i.e. 0 will be moved outside, and at the rightmost place or LSB, 0 will be inserted as shown below.

The given binary number (00100011) represents 35 in the decimal system. And the binary number after logical shift left (01000110) represents 70 in a decimal system. Since 35 * 2 = 70. Therefore, we can say that the arithmetic shift left multiplies the number by 2.

To implement the arithmetic shift left micro-operation, we use the **ashl **symbol.

For example, R1 -> ashl R1.

This command means the 8 bits present in the R1 register will be arithmetic shifted left, and the result will be stored in register R1.

#### Arithmetic Shift Right

Each bit in the register is shifted to the right one by one in this shift micro-operation. The least significant bit (LSB) is moved outside the register, and the place of the most significant bit (MSB) is filled with the previous value of MSB.

For example, in the below data, there are 8 bits 10100011. When we perform an arithmetic shift right on these bits, all these bits will be shifted towards the right. The LSB or the rightmost bit i.e. 1 will be moved outside, and at the leftmost place or MSB, the previous MSB value, i.e. 1, will be inserted as shown below.

Arithmetic right shift divides the number by 2.

To implement the arithmetic shift right micro-operation, we use the **ashr **symbol.

For example, R1 -> ashr R1.

This command means the 8 bits present in the R1 register will be arithmetic shifted right, and the result will be stored in register R1.

Next, we will study circular shift micro-operation.

### Circular Shift

The circular shift, also known as the rotate shift, moves the bits in the register's sequence around both ends, thus ensuring no loss of information. There are two ways to implement the circular shift.

- Circular Shift Left
- Circular Shift Right

Letâ€™s discuss both of them one by one.

#### Circular Shift Left

Each bit in the register is shifted to the left one by one in this shift micro-operation. After shifting, the least significant bit (LSB) place becomes empty, so it is filled with the value at the most significant bit (MSB).

For example, in the below data, there are 8 bits 00010100. When we perform a circular shift left on these bits, all these bits will be shifted towards the left. The MSB or the leftmost bit i.e. 0 will be placed at the rightmost place or LSB as shown below.

To implement the circular shift left micro-operation, we use the **cil **symbol.

For example, R1 -> cil R1.

This command means the 8 bits present in the R1 register will be circular shifted left, and the result will be stored in register R1.

Next, we will discuss circular shift right micro-operation.

#### Circular Shift Right

Each bit in the register is shifted to the right one by one in this shift micro-operation. After shifting, the most significant bit (MSB) place becomes empty, so it is filled with the value at the least significant bit (LSB).

For example, in the below data, there are 8 bits 00010100. When we perform a circular shift right on these bits, all these bits will be shifted towards the right. The LSB or the leftmost bit, i.e. 0, will be placed at the rightmost place or MSB.

To implement the circular shift right micro-operation, we use the **cir **symbol.

For example, R1 -> cir R1.

This command means the 8 bits present in the R1 register will be circular shifted right, and the result will be stored in register R1.

Read about __Bitwise Operators in C__.