Originally published by Robert Beisert at fortcollinsprogram.robert-beisert.com

CS 101 – Registers

We now have some understanding of what bits are and how we work with them, but how do we store them?

Registers

A register is a computer element which stores bytes of data. There are several ways to build these registers, but their core function is to hold onto data for future use.

Consider the following pseudocode:

a = 1

b = a + 2

c = b + a

print a, b, c

To perform these operations, we need three registers, which we will call “a”, “b”, and “c”.

First, we store the value 1 in the register a.

Next, we store the value in register a PLUS the value 2 into register b. Register b, therefore, now has the value 3.

Third, we store the sum of the value in register a and the value in register b into register c. Register c now has the value 4.

When we print the register values, we get 1, 3, 4.

 

And there you have it. Without registers, this series of steps would be almost impossible to reproduce (without some parallel-threaded operation, but that’s another story).

Register Shifts

Speaking of registers, we have a non-logical, non-mathematical operation we can perform on bits: the shift.

Shifts fall into two categories: logical shifts and arithmetic shifts.

Logical shifts simply move the bits over one space, then fill in the blanks with 0. For example, a Logical Shift Left on 11101101 would result in the value 11011010, while a Logical Shift Right would result in 01110110.

Arithmetic shifts are a bit trickier. When we perform this shift, we fill in any blanks with the most significant bit (read: left-most bit) from the original. The Arithmetic Shift Left on the value 11101101 is therefore 11011011, and the Arithmetic Shift Right is 11110110.

Note: There are other possible arithmetic shifts. Some computers shift everything but the most significant bit, to prevent overflow (read: going from positive to negative, or vice versa). In addition, very few computers perform a left arithmetic shift, because it’s confusing.

Circular Shift

Using logical shifts with a bit of ingenuity can produce a “circular shift”, where we just shift the code in a circle. For example, a left circular shift on 0110:

0110

1100

1001

0011

0110

This is accomplished by feeding the most significant bit (or least significant bit, for a right shift) back into the other side. That would look something like this:

Apologies for the poor MSpaint quality. Shaky hands and a mediocre mouse...
Apologies for the poor MSpaint quality. Shaky hands and a mediocre mouse…

Acronyms

MSB          Most Significant Bit

LSB           Least Significant Bit

SLL           Shift Left – Logical

SRL          Shift Right – Logical

SRA          Shift Right – Arithmetic