\1
Back to Data Storage

Data Storage: Hexadecimal & Binary Shifts

OCR GCSE Computer Science (J277) - Topic 1.2.1: Units, Binary, Hexadecimal, Conversions, Binary Shifts

Starter Activity (5 mins)

What do you already know or what are your best guesses?

Lesson Outcomes

By the end of this lesson, you should be able to:

  • Understand the purpose of hexadecimal as a shorthand for binary.
  • Know that one hexadecimal digit represents 4 binary bits (a nibble).
  • Convert between binary, denary, and hexadecimal (up to 2 hex digits / 8 bits).
  • Understand the effect of a binary shift left (multiplication by powers of 2).
  • Understand the effect of a binary shift right (division by powers of 2, integer division).
  • Identify potential issues like overflow with binary shifts.

Introduction: Why Not Just Binary?

We know computers use binaryA base-2 number system using only 0s and 1s. The fundamental language of computers. (0s and 1s) at their core. However, long strings of binary can be difficult for humans to read, write, and remember accurately. For example, the binary number 1101010110111100 is quite a mouthful!

HexadecimalA base-16 number system using digits 0-9 and A-F. Often used as a shorthand for binary because 1 hex digit = 4 bits. (often shortened to "hex") is a base-16 number system that provides a more human-friendly way to represent binary data. It's widely used in computing for this reason.

We'll also explore binary shiftsAn operation that moves all bits in a binary number to the left or right. Used for quick multiplication or division by powers of 2., which are simple operations on binary numbers that have powerful mathematical effects.

Task 1: Understanding Hexadecimal

a) What is the base of the hexadecimal number system?

b) How many binary bits does one hexadecimal digit represent?

c) Complete the table for the 16 hexadecimal digits and their denary equivalents:

Hex | DenaryHex | DenaryHex | DenaryHex | Denary 0 = 04 = 48 = 8C = 1 = 15 = 59 = 9D = 2 = 26 = 6A = E = 3 = 37 = 7B = F =

Task 2: Denary to Hexadecimal Conversion

To convert a denary number to hexadecimal:

  1. Divide the denary number by 16.
  2. Write down the remainder (this will be a hex digit, 0-9 or A-F).
  3. Take the whole number part of the result and divide it by 16 again.
  4. Repeat until the whole number part is 0.
  5. The hexadecimal result is the remainders read from bottom to top.

Hexadecimal Output:

Task 3: Hexadecimal to Denary Conversion

To convert a 2-digit hexadecimal number (e.g., A5) to denary:

  1. Take the first hex digit, convert it to its denary equivalent, and multiply by 16.
  2. Take the second hex digit and convert it to its denary equivalent.
  3. Add the two results together.

Denary Output:

Task 4: Binary to Hexadecimal Conversion

To convert an 8-bit binary number to hexadecimal:

  1. Split the 8-bit binary number into two 4-bit groups (nibbles).
  2. Convert each 4-bit nibble into its equivalent hexadecimal digit.

Hexadecimal Output:

Task 5: Hexadecimal to Binary Conversion

To convert a 2-digit hexadecimal number to an 8-bit binary number:

  1. Take the first hexadecimal digit and convert it to its 4-bit binary equivalent.
  2. Take the second hexadecimal digit and convert it to its 4-bit binary equivalent.
  3. Combine the two 4-bit groups to form an 8-bit binary number.

8-bit Binary Output:

Interactive Hex Color Mixer & Matcher

Experiment with hexadecimal color codes! Input two hex digits (00-FF) for Red, Green, and Blue to see the color. Then, try to match the target color.

Your Color Mixer

Your Hex: #000000

Match the Target Color

Target Hex: (Hidden)

Task 6: Binary Shift Left Simulation

A binary shift left moves all bits in a binary number one or more places to the left. New empty positions on the right are filled with zeros. Each shift left multiplies the denary value by 2.

Original Denary:

Shifted Binary:

Shifted Denary:

Task 7: Binary Shift Right Simulation

A binary shift right moves all bits one or more places to the right. Bits shifted off the right end are lost. New empty positions on the left are usually filled with zeros (for unsigned numbers). Each shift right divides the denary value by 2 (integer division).

Original Denary:

Shifted Binary:

Shifted Denary:

Task 8: Binary Shift Practice

1. Shift 00101100 left by 2 places.

2. Shift 11100000 right by 3 places.

3. What happens if you shift 10000001 left by 1 place (in an 8-bit register)?

Binary Shift Detective: Spot the Mistake!

Below are some binary shift operations with incorrect results or explanations. Identify the mistake and explain why it's wrong in the box provided.

Problem 1:

An 8-bit binary number 11001010 (denary 202) is shifted left by 1 place.

Provided "Solution":

  • Original: 11001010 (Denary: 202)
  • Shifted Binary: 10010100
  • New Denary: 404 (because 202 * 2 = 404)

Problem 2:

An 8-bit binary number 00001101 (denary 13) is shifted right by 1 place.

Provided "Solution":

  • Original: 00001101 (Denary: 13)
  • Shifted Binary: 00000110
  • New Denary: 6.5 (because 13 / 2 = 6.5)

Task 9: Practice Worksheets (Excel)

For more extensive practice, download and complete the following Excel worksheets. They contain many more questions to help you master these conversions and shifts.

Note: You will need Microsoft Excel or a compatible spreadsheet program to open these files.

Real-World Context

Where is Hexadecimal Used?

  • HTML/CSS Color Codes: E.g., #FF0000 for red, #00FF00 for green. Each pair of hex digits represents the intensity of Red, Green, and Blue.
  • MAC Addresses: Unique hardware identifiers for network cards, often shown in hex (e.g., 00:1A:2B:3C:4D:5E).
  • Memory Dumps & Debugging: Programmers use hex to view the raw contents of computer memory, as it's more compact than binary.
  • Error Codes: Some systems display error codes in hexadecimal.

Why are Binary Shifts Useful?

  • Fast Multiplication/Division: Shifting is a very fast way for the CPU to multiply or divide by powers of 2, much quicker than complex multiplication/division algorithms.
  • Low-Level Programming: In assembly language or systems programming, shifts are used for manipulating individual bits, setting flags, or packing data efficiently.
  • Graphics & Audio Processing: Shifts can be used in algorithms for scaling images, adjusting audio volumes, or other signal processing tasks.

Myth Busters

Myth 1: "Hexadecimal is a different way of storing numbers in a computer."

Reality: Computers fundamentally store and process everything in binaryA base-2 number system using only 0s and 1s. The fundamental language of computers.. Hexadecimal is simply a more human-readable representationHow data is shown or interpreted. Hexadecimal is a representation of binary data. of binary data. It's a shorthand, not a different storage method at the hardware level.

Myth 2: "Binary shifts always give the exact mathematical result for multiplication and division."

Reality: While a left shift accurately multiplies by powers of 2 (unless overflowAn error that occurs when the result of a calculation is too large to fit in the available number of bits. occurs), a right shift performs integer divisionDivision where any fractional part (remainder) is discarded. E.g., 7 / 2 = 3 in integer division.. This means any fractional part of the result is lost (e.g., 00000111 (7) shifted right by 1 becomes 00000011 (3), not 3.5). Also, overflow can happen with left shifts if the result is too large for the number of bits available.

Exam Practice Questions

1. Convert the denary number 173 into hexadecimal. Show your working. [2 marks]

Mark Scheme:

  • Correctly showing division by 16 and noting remainders (1 mark). E.g., 173 / 16 = 10 remainder 13 (D); 10 / 16 = 0 remainder 10 (A).
  • Correct final hexadecimal answer: AD (1 mark).

2. Convert the hexadecimal number B4 into an 8-bit binary number. [1 mark]

Mark Scheme:

  • Correct 8-bit binary: 10110100 (1 mark). (B=1011, 4=0100)

3. An 8-bit binary number 00110101 is shifted two places to the left.

(i) State the binary number after the shift. [1 mark]

(ii) State the denary equivalent of the original number and the shifted number. [2 marks]

(iii) Describe the arithmetic effect of this shift. [1 mark]

Mark Scheme:

  • (i) Shifted binary: 11010100 (1 mark).
  • (ii) Original denary: 53. Shifted denary: 212 (1 mark for each correct denary).
  • (iii) Multiplied the original number by 4 (or by 2 twice) (1 mark).

4. Convert the denary number 97 into an 8-bit binary number. Show your working or method. [2 marks]

Mark Scheme:

  • Showing a valid method (e.g., place value subtraction, repeated division by 2 and collecting remainders) (1 mark).
  • Correct 8-bit binary answer: 01100001 (1 mark).

5. Convert the 8-bit binary number 11001010 into denary. Show your working. [2 marks]

Mark Scheme:

  • Showing correct application of place values (e.g., 128+64+0+0+8+0+2+0) (1 mark).
  • Correct denary answer: 202 (1 mark).

6. An 8-bit binary number 01011000 is shifted three places to the right.

(i) State the binary number after the shift. [1 mark]

(ii) Describe the arithmetic effect of this shift on the original number. [1 mark]

Mark Scheme:

  • (i) Shifted binary: 00001011 (1 mark).
  • (ii) Divided the original number by 8 (or by 2 three times) (integer division) (1 mark).

7. Explain one reason why hexadecimal numbers are often used in computer science. [2 marks]

Mark Scheme:

  • It is a shorthand for binary / more compact than binary / easier for humans to read/understand than binary (1 mark).
  • Because one hex digit represents 4 binary bits / two hex digits represent one byte (1 mark). OR Example of use e.g. MAC addresses, HTML colours, memory dumps (1 mark).

(Max 2 marks)

Lesson Score

Click the button below to calculate your total score for the interactive tasks.

Key Takeaways

  • Hexadecimal (Base-16): Uses digits 0-9 and A-F (A=10, B=11, C=12, D=13, E=14, F=15).
  • Hex & Binary Link: One hexadecimal digit represents exactly 4 binary bits (a nibbleA group of 4 binary bits.). This makes it a convenient shorthand for binary.
  • Conversions: You need to be able to convert between denary, binary, and hexadecimal (for numbers up to 255 / FF / 11111111).
  • Binary Shift Left: Multiplies the number by 2 for each place shifted. Zeros are added on the right. Watch out for overflowAn error that occurs when the result of a calculation is too large to fit in the available number of bits.!
  • Binary Shift Right: Divides the number by 2 for each place shifted (integer division - fractional parts are lost). Zeros are usually added on the left for unsigned numbers.

What's Next?

You've now explored hexadecimal and binary shifts!

Next, we'll look at:

  • Character sets (ASCII, Unicode) and how text is represented (Spec 1.2.2).
  • Image representation (pixels, colour depth, resolution) (Spec 1.2.3).
  • Sound representation (sampling, sample rate, bit depth) (Spec 1.2.4).

Extension Activities

1. Hexadecimal Arithmetic

Programmers sometimes need to perform simple addition or subtraction directly in hexadecimal, especially when working with memory addresses.

Research Task: How do you add two hexadecimal numbers, for example, A5 + 1B? What happens if a sum in one column exceeds 'F' (15)?

2. Logical vs. Arithmetic Shifts

We've looked at logical shifts (filling with zeros). For signed binary numbers (representing positive and negative values, often using Two's Complement), an arithmetic shift right behaves differently to preserve the sign bit.

Research Task: How does an arithmetic shift right differ from a logical shift right, particularly for negative numbers in Two's Complement form? Why is this distinction important?