Logical Shifts: Shuffling Bits for Fun and (Digital) Profit!

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.

Left Shift: Shoving Bits and Making Numbers Bigger (Usually)

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!

Example: One-Place Left Shift

Before Shift01101010
After 1-Place Left Shift11010100

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.

Worked Examples Icon Worked examples

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

Right Shift: Shoving Bits the Other Way (and Making Numbers Smaller-ish)

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.

Worked Examples Icon Worked examples

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

Guide Icon Guided Practice

1. Perform a 2-place left shift on the binary number 110010.

Add two 0s on the right-hand side.

110010 becomes 11001000

2. Perform a 1-place right shift on the binary number 10110.11.

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

3. Perform a 2-place left shift on the binary number 0.01101.

Move the binary point two places to the right. Add 0s if needed.

0.01101 becomes 1.101

4. Perform a 3-place right shift on the binary number 101011.1.

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

5. State the effect of a 4-place left shift on a binary number.

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.

6. State the effect of a 2-place right shift on a binary number.

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.

Task List Icon Independent Practice

  1. Perform a 1-place left shift on the binary number 101101.
  2. Perform a 3-place right shift on the binary number 1101001.
  3. Perform a 2-place left shift on the binary number 00111010. Give your answer as an 8-bit number.
  4. Perform a 4-place right shift on the binary number 10110000.
  5. State the effect of a 3-place left shift on a binary number.
  6. State the effect of a 4-place right shift on a binary number.
  7. Perform a 4-place left shift on the binary number 10.11 (fixed-point).
  8. Perform a 2-place right shift on the binary number 101.01 (fixed-point).
  9. Perform a 5-place left shift on the binary number 00011.011. Give your answer as an 8-bit number (assuming the decimal point shifts with the bits).
  10. Perform a 3-place right shift on the binary number 110110.101.

Answers

  1. 1011010
  2. 0001101
  3. 11101000
  4. 00001011
  5. Multiplies the integer part by 23 (8).
  6. Divides the integer part by 24 (16), discarding any remainder.
  7. 101100.
  8. 1.0101
  9. 11011000.
  10. 110.110101

Bitwise Operators: Playing with the Very Fabric of Binary!

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:

NOT: The Great Bit Flipper!

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.

AND: The Fussy One!

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".

OR: The Agreeable One!

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 (Exclusive OR): The Picky One!

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.

Worked Examples Icon Worked examples

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.

1101
0101
0101

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.

1101
0101
1101

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.

1101
0101
1000

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.

Guide Icon Guided Practice

1. Perform a bitwise NOT operation on 101100.

Reverse each of the bits.

101100 becomes 010011

2. Perform a bitwise AND operation on 11011 with the mask 10101.

Step 1: put the numbers above each other, like in an addition.

11011
10101
10001

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

3. Perform a bitwise OR operation on 110010 with the mask 011011.

Step 1: put the numbers above each other.

110010
011011
111011

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

4. Perform a bitwise XOR operation on 110011 with the mask 101100.

Step 1: put the numbers above each other.

110011
101100
011111

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

Task List Icon Independent Practice

  1. Perform a bitwise NOT operation on 11001100.
  2. Perform a bitwise NOT operation on 00101010.
  3. Perform a bitwise AND operation on 10101010 with the mask 11001100.
  4. Perform a bitwise AND operation on 01101101 with the mask 00001111.
  5. Perform a bitwise OR operation on 10010011 with the mask 01100110.
  6. Perform a bitwise OR operation on 00111100 with the mask 11000011.
  7. Perform a bitwise XOR operation on 10110011 with the mask 01010101.
  8. Perform a bitwise XOR operation on 11110000 with the mask 11111111.

Answers

  1. 00110011
  2. 11010101
  3. 10001000
  4. 00001101
  5. 11110111
  6. 11111111
  7. 11100110
  8. 00001111