 In this video, we're going to be looking at shift and rotate operations. These are probably unusual operations that you haven't seen before, because they're not used in a whole lot of high-level programming. We use them frequently about the same time that they're generally used in about the same places that logical operations are used. These give us another way to manipulate the bits inside a number without really looking at what is this as a number. The shift and rotate operations are designed to take the bits in a number and kind of move them back or forth. MIPS gives a... There are five kind of operations we can look at for doing this sort of thing. Start with the first ones and kind of work our way towards the bottom. A shift left operation... If I have a number, say... I've got... Say this, B5 in hexadecimal. I'm going to shift all the bits in this number left three places. So that means that I'll have three bits here that are just going to fall off the end of my number. So I'll have a 1, 0, 1, 0, 1. And now I've got three more places. I have to fill them with something, so I'll stick zeros in there. So this part of my number got shifted left. This part of the number, the most significant three bits fell off, and instead I added in three zeros on the right-hand side. So that's what shifting left will do. Do this with a 32-bit number. You just have more bits to shift. Likewise, if I'm going to use a shift right logical, it'll do about the same thing. If I shift these bits right three places, I'd get 1, 0, 1, 1, 0. And then because these three bits have fallen off the left, then because these three bits have fallen off the right-hand side, I have three empty spaces over here that I would fill with zeros. So again, these five bits moved right three places. I lost three bits on the right-hand side because they fell off my number, so I added three zeros in on the left. This is a shift right logical operation, though, because we're really just picking up the bits and moving them. We're not worrying about what is in this number. But sometimes we do care. Sometimes we're interested in key. Sometimes we are interested in key. Sometimes we are interested in preserving the sign of our number. So instead, I could have a shift right arithmetic instruction. This will ensure whatever the results of my number are that they're still relatively comparable to what I had to begin with. The sign will stay the same. So here, if I have that same number, B5, I shift it right three places, all of these five bits are still going to end up over here the way they were before. That much isn't going to change. But this time, and I'm still going to throw away these three bits, but this time instead of just putting in zeros on the left-hand side, I'm going to copy out this sign bit. So if that sign bit was a zero, I would still have three zeros there, but in this case, the sign bit is a one, so I end up with three leading ones. That means that since this number was negative before, it still looks negative afterwards. Rotate operations are similar. We're still shifting our bits left to right, but this time we're not just going to discard bits. We will pull them back in on the other side. So if I start with that same number and I decide to rotate them this time, then these five bits will end up here the same way they did before. But now these three bits will get moved in on the right-hand side. That way I'm not just shifting in zeros. I'm now shifting in something that's maybe interesting. This would be especially useful if I want to keep lots of bits in my number. They're doing something productive for me, like perhaps a random number generator. I don't just want to pull in zeros maybe. That might not make my number interesting enough. I'd rather pull in some extra stuff. Now a rotate-right operation will pretty much work exactly the same way. We'll just rotate our bits. We'll shift our bits right and rotate the ones that fall off back in on the left. So these five bits will be moved over three places. And then we have the one-zero-one that previous to the right-hand side now be brought back in on the left. The MIPS architecture allows you to shift your bits up to 31 places left or right. This is a 32-bit architecture, so all of our numbers are 32 bits long. So shifting up to 31 places works just fine because anything more, and you just completely throw away your number. You might as well just stick all... you'd be able to just stick zeros in. If you were to... you could just as easily stick zeros in for a shift-left logical or a shift-right logical. For your shift-right arithmetic, well, that won't change anything, all of your number. For a shift-right arithmetic, that won't change anything. You'll either have all zeros or all ones, even just shifting 31 places. So you're able to shift as many places left or right as you'd like. Chances are you won't need to use one of these anytime soon, but they are kind of interesting to see, and if you have to write a random number generator, then chances are you will get to do these. But if you have to write a random number generator, or you're building packets of something where you're trying to stuff... or you're building packets of something where you're trying to stuff data into the smallest chunk of memory possible, then you may get to use one of these.