-[Craig] In this video, we take a look at logical and arithmetic binary shifts.
(uplifting piano jingle)
So with a binary shift, all we're going to do is shift all the bits over to the left or to the right.
So let's consider the binary number 22 shown here.
We're going to left shift it by one bit.
And then what happens is the bit in the 128 column is going to disappear off the left-hand side.
The bit that's in the 64 column is going to move over,
left shifted one position so that it becomes a zero.
The bit in the 32 position is going to shift one place to the left.
The bit in the 16 position shifts to the left.
The bit in the eight position shifts one space to the left.
The same with the four position, the two position and the one position.
We end up with an empty slot on the far right,
which we can simply pad with a zero.
So if we left shift the number 22 one place,
what we get back is 32 plus 8, which is 40 plus 4, which is 44.
So 22 has become 44.
In effect, we've multiplied the number by two.
Now, you don't just have to shift by one bit,
you could shift by two bits or more.
The principle is the same.
The bits are going to disappear off the left-hand side,
they're going to get padded on the right-hand side,
and any in the middle are going to be shifted over that number of places.
A binary right shift is exactly the same,
just in the other direction.
So we start by shifting each digit right the number of places we need.
So we're going to do a right shift of one.
So the 0 from the 128 column ends up in the 64 column.
The 0 in the 64 column ends up right shifted one place in the 32, and so on and so forth.
The final nought in the one column disappears off the right-hand side,
and we pad the leading space under the 128 column with a 0.
In effect, we have halved the number.
We have divided it by two
when we shift to the right by one bit.
Just like the shift to the left,
we don't have to shift to the right by just one bit at a time.
As we can see, binary left and right shift can be used to multiply and divide binary numbers.
So now we've been through logical shifts,
let's consider logical versus arithmetic shifts.
Now note that in both examples we've shown,
the logical shift pads the empty space with a zero shown in purple,
While the resulting number still works, this is only the case for unsigned binary.
As we already know, in two's complement signed binary,
the most significant bit is reserved as the sign bit,
where zero represents a positive number and one represents a negative number.
In this situation,
a logical right shift is no use,
as the 1 from the most significant bit column, which represents minus 128,
gets shifted one place to the right,
and the space left behind gets padded or filled by a zero,
thus suddenly turning the number positive.
However, if we were to use an arithmetic right shift instead of a logical one,
the vacant MSB is filled with the value of the previous MSB,
which has now been shifted one position to the right,
so the one is maintained and the negative status is retained.
So we can't divide negative numbers with a logical right shift,
but we can divide negative numbers with an arithmetic right shift.
Note that using a left arithmetic shift to try to perform multiplication of negative numbers
still doesn't work because the most significant bit is not preserved, as can be seen here.
Okay, so that's everything you need to know about logical and arithmetic left and right shifts.
So pause the video and take some notes.
(uplifting piano jingle)