Welcome to the World of Number Translation!
In Computer Science, we often need to talk to computers in their own language. While we love the number 10 (Decimal), computers only understand Binary (Base 2), and programmers often use Hexadecimal (Base 16) as a shorthand. In this chapter, you will learn how to be a "number translator," moving between these three systems with ease.
Don't worry if this seems a bit "maths-heavy" at first! Once you learn the simple patterns and tricks, it becomes as easy as following a recipe. We will focus on numbers between 0 and 255, which is the range of a single byte (8 bits).
1. Prerequisite: Knowing Your Bases
Before we convert, let’s quickly recap our three systems:
- Decimal (Base 10): Uses digits 0-9. This is what you use every day.
- Binary (Base 2): Uses only 0 and 1.
- Hexadecimal (Base 16): Uses 0-9, then letters A to F to represent 10 to 15.
Quick Review Table for Hex:
A = 10 | B = 11 | C = 12 | D = 13 | E = 14 | F = 15
2. Converting Binary to Decimal
The easiest way to do this is using a place value table. Since we are working with 8-bit binary, we draw a table with 8 columns, starting from 1 on the right and doubling every time as we move left.
Step-by-Step Process:
- Draw your table: 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1
- Write the binary number underneath the table.
- Add up all the numbers that have a 1 underneath them.
Example: Convert 10101000 to Decimal
128(1) + 64(0) + 32(1) + 16(0) + 8(1) + 4(0) + 2(0) + 1(0)
Calculation: \( 128 + 32 + 8 = 168 \)
Result: 168
Key Takeaway: Just "turn on" the place values where there is a 1 and ignore the 0s!
3. Converting Decimal to Binary
Think of this like a "greedy" game. You want to see which of the binary place values (128, 64, etc.) fit into your decimal number.
Step-by-Step Process:
- Start with your 8-bit table: 128, 64, 32, 16, 8, 4, 2, 1.
- Starting from the left (128), ask: "Is my number bigger than or equal to this place value?"
- If YES: Write a 1, and subtract that value from your total.
- If NO: Write a 0 and move to the next column.
Example: Convert 75 to Binary
- Does 128 fit into 75? No. (0)
- Does 64 fit into 75? Yes. (1). Remaining: \( 75 - 64 = 11 \)
- Does 32 fit into 11? No. (0)
- Does 16 fit into 11? No. (0)
- Does 8 fit into 11? Yes. (1). Remaining: \( 11 - 8 = 3 \)
- Does 4 fit into 3? No. (0)
- Does 2 fit into 3? Yes. (1). Remaining: \( 3 - 2 = 1 \)
- Does 1 fit into 1? Yes. (1). Remaining: 0
Result: 01001011
Common Mistake: Forgetting to write the 0s! An 8-bit binary number must have 8 digits, even if the first ones are 0.
4. Binary and Hexadecimal: The "Nibble" Trick
Hexadecimal looks scary, but there is a secret shortcut! One Hex digit represents exactly 4 bits (a nibble).
Converting Binary to Hex:
- Split your 8-bit binary number into two 4-bit parts (nibbles).
- Convert each 4-bit part into decimal (values 0-15).
- Change any number from 10-15 into the correct Hex letter (A-F).
Example: Convert 11010011 to Hex
Left Nibble: 1101 → \( 8+4+1 = 13 \). In Hex, 13 is D.
Right Nibble: 0011 → \( 2+1 = 3 \). In Hex, 3 is 3.
Result: D3
Converting Hex to Binary:
- Take each Hex digit separately.
- Convert it into a 4-bit binary number.
- Join them together.
Example: Convert 2F to Binary
2 → 0010
F (15) → 1111
Result: 00101111
Did you know? Programmer's use Hex because it's much shorter to write than binary. "FF" is much easier to read than "11111111"!
5. Converting Hexadecimal to Decimal
For the AQA 8525 syllabus, you only need to go up to FF (255). This means you only ever deal with two Hex digits.
Step-by-Step Process:
- The right digit is the "Units" (multiplied by 1).
- The left digit is the "Sixteens" (multiplied by 16).
- Multiply them and add them together.
Example: Convert 3B to Decimal
- Left digit: 3. Calculation: \( 3 \times 16 = 48 \)
- Right digit: B (11). Calculation: \( 11 \times 1 = 11 \)
- Add them: \( 48 + 11 = 59 \)
Result: 59
6. Converting Decimal to Hexadecimal
There are two ways. You can go "Decimal → Binary → Hex" (using the nibble trick), or use division.
The Division Method:
- Divide your decimal number by 16.
- The answer is your left Hex digit.
- The remainder is your right Hex digit.
Example: Convert 162 to Hex
- \( 162 \div 16 = 10 \). (10 in Hex is A).
- The remainder is 2. (2 in Hex is 2).
Result: A2
Quick Review Box:
- Binary place values: 128, 64, 32, 16, 8, 4, 2, 1
- Hex place values: 16, 1
- Max value for 8-bit/2-digit Hex: 255 (FF)
Final Summary: Which method should I use?
- Binary ↔ Decimal: Use the 128...1 table.
- Binary ↔ Hex: Split into 4-bit nibbles. This is the fastest way!
- Hex ↔ Decimal: Use the "multiply/divide by 16" method, or convert to binary first if you find that easier.
Top Tip: Always double-check your addition! Most marks are lost on simple calculation errors rather than not understanding the process.