Ah, logical shifts. The digital equivalent of lining up all your toy soldiers and then nudging them a few spaces to the left or right. It might sound like a terribly boring pastime, but in the bizarre world inside your computer, it's a rather neat way of performing multiplication and division without all the complicated fuss.
A left shift is exactly what it sounds like: you take all the bits in your binary number and shove them a certain number of places to the left. The empty spaces you create on the right? Well, they just get filled with good old zeros. It's like the binary bits are all moving up in the world, getting extra zeros as their reward.
Now, here's the clever bit: every time you shift the bits one place to the left, you're actually multiplying the number by 2. A two-place left shift? That's a multiplication by 4 (which is 22, for those who like their powers of two). A three-place shift? You've guessed it – multiplied by 8 (or 23). It's a surprisingly efficient way to make your binary numbers bigger!
| Before Shift | 0 | 1 | 1 | 0 | 1 | 0 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|
| After 1-Place Left Shift | 1 | 1 | 0 | 1 | 0 | 1 | 0 | 0 |
As you can see, every bit shuffled one place to the left. The '0' at the beginning vanished, and a '0' appeared at the end, keeping it as an 8-bit number.
a) Perform a 1-place left shift on the binary number 00101101.
Add one 0 on the right-hand side: 001011010
b) Perform a 2-place left shift on the binary number 0011.011.
Move the binary point two places to the right. If more bits are needed, add 0s: 001101.1
c) Show the effect of a 2-place left shift on the binary number 00111010.
Step 1: work out the denary value before the shift: 00111010 = 58
Step 2: add two 0s on the right-hand side: 0011101000
Step 3: work out the denary value after the shift: 0011101000 = 232
This is a 2-place shift, so the number is multiplied by 4, 58 × 4 = 232
Now, if shoving bits to the left makes things bigger, then it stands to reason that shoving them to the right does the opposite. That's precisely what a right shift is all about: taking all those binary digits and nudging them a certain number of places to the right.
Each time you perform a right shift by one place, you're essentially dividing the number by 2. A two-place shift? Division by 4 (that's 22 again!). Three places? Division by 8 (or 23). It's a handy shortcut for making those binary numbers smaller.
a) Perform a 1-place right shift (binary point shift) on the binary number 10110110.
Step 1: Add the binary point: 10110110.
Step 2: Move the binary point one place to the left: 1011011.0
b) Perform a 2-place right shift (binary point shift) on the binary number 01100111.
Step 1: Add the binary point: 01100111.
Step 2: Move the binary point two places to the left: 011001.11
c) Show the effect of a 3-place logical right shift on the binary number 11001000.
Step 1: Work out the denary value before the shift: 11001000 = 200
Step 2: Shift the bits 3 places to the right: 00011001
Step 3: Work out the denary value after the shift: 00011001 = 25
This is a 3-place shift, so the number is divided by 8: 200 ÷ 8 = 25
Add two 0s on the right-hand side.
110010 becomes 11001000
Step 1: Add the binary point (if not present). 10110.11
Step 2: Move the binary point one place to the left.
10110.11 becomes 1011.011
Move the binary point two places to the right. Add 0s if needed.
0.01101 becomes 1.101
Step 1: Add the binary point (if not present). 101011.1
Step 2: Move the binary point three places to the left.
101011.1 becomes 101.0111
Each shift left multiplies it by 2, so a 1-shift is multiplied by 2, a 2-shift is multiplied by 4, a 3-shift is multiplied by 8, and a 4-shift is multiplied by 16.
Each shift right divides the number by 2, so a 1-shift divides the number by 2, a 2-shift divides the number by 4, a 3-shift divides the number by 8, a 4-shift divides the number by 16.
Answers
Ah, bitwise operators. If logical shifts are like nudging rows of soldiers, these are like getting up close and personal with each individual soldier and giving them a little digital tweak. These operators work directly on the individual bits (the 0s and 1s) within binary numbers. They're not your everyday arithmetic (+, -, ×, ÷) – these are more about logical tinkering.
There are a few key players in this bitwise game: NOT, AND, OR, and XOR (which sounds a bit like a futuristic grunt). They operate in the same way as those logic gates you might have encountered. Let's have a quick peek at each of these digital meddlers:
The NOT operator is a simple beast. It takes a single binary number and flips every single bit. If it's a 0, it becomes a 1. If it's a 1, it becomes a 0. It's like a digital photo negative for your bits.
The AND operator is a bit more demanding. It takes two binary numbers and compares them bit by bit. The result for each corresponding pair of bits is only a 1 if both of the original bits were also 1. If even one of them is a 0, the result for that bit position is a 0. It's like saying "both have to be true to get a true result".
The OR operator is a more agreeable chap. It also takes two binary numbers and compares them bit by bit. The result for each pair of bits is a 1 if either one of the original bits was a 1 (or if both were 1). It only gives a 0 if both of the original bits were 0. It's like saying "if at least one is true, the result is true".
XOR, or exclusive OR, is a bit picky. It takes two binary numbers and compares them bit by bit. The result for each pair of bits is a 1 if the two original bits are different.
When you're using AND, OR, or XOR, you'll always need two binary numbers to work with. Sometimes, one of these numbers might be referred to as a 'mask'. Don't let that fancy term scare you – it's just the binary number you're using to selectively change or examine the bits of the other number. Think of it like using a stencil to only paint certain parts of a wall.
a) Perform the bitwise NOT operation on 0110.
Reverse each of the bits to give 1001.
b) Perform a bitwise AND operation on 1101 with the mask 0101.
Step 1: put the numbers above each other, like in an addition.
| 1 | 1 | 0 | 1 |
| 0 | 1 | 0 | 1 |
| 0 | 1 | 0 | 1 |
Step 2: compare each pair of bits in turn. If they are both 1, the result is 1, otherwise the result is 0.
c) Perform a bitwise OR operation on 1101 with the mask 0101.
Step 1: put the numbers above each other.
| 1 | 1 | 0 | 1 |
| 0 | 1 | 0 | 1 |
| 1 | 1 | 0 | 1 |
Step 2: compare each pair of bits in turn. If either or both are 1s, the result is 1, otherwise the result is 0.
d) Perform a bitwise XOR operation on 1101 with the mask 0101.
Step 1: put the numbers above each other.
| 1 | 1 | 0 | 1 |
| 0 | 1 | 0 | 1 |
| 1 | 0 | 0 | 0 |
Step 2: compare each pair of bits in turn. If the bits are different, the result is 1. If they are the same (both 0s or both 1s), the result is 0.
Reverse each of the bits.
101100 becomes 010011
Step 1: put the numbers above each other, like in an addition.
| 1 | 1 | 0 | 1 | 1 |
| 1 | 0 | 1 | 0 | 1 |
| 1 | 0 | 0 | 0 | 1 |
Step 2: compare each pair of bits in turn. If both are 1, the result is 1, otherwise the result is 0.
The result is 10001
Step 1: put the numbers above each other.
| 1 | 1 | 0 | 0 | 1 | 0 |
| 0 | 1 | 1 | 0 | 1 | 1 |
| 1 | 1 | 1 | 0 | 1 | 1 |
Step 2: compare each pair of bits in turn. If either or both are 1s, the result is 1, otherwise the result is 0.
The result is 111011
Step 1: put the numbers above each other.
| 1 | 1 | 0 | 0 | 1 | 1 |
| 1 | 0 | 1 | 1 | 0 | 0 |
| 0 | 1 | 1 | 1 | 1 | 1 |
Step 2: compare each pair of bits in turn. If both bits are the same (both 0s or both 1s), the result is 0, otherwise the result is 1.
The result is 011111
Answers