Download presentation
Presentation is loading. Please wait.
1
Data Representation :: Binary & Hexadecimal
Last modified: 7th July 2019
2
www.drfrostmaths.com ? Everything is completely free.
Why not register? Registering on the DrFrostMaths platform allows you to save all the code and progress in the various Computer Science mini-tasks. It also gives you access to the maths platform allowing you to practise GCSE and A Level questions from Edexcel, OCR and AQA. With Computer Science questions by: Your code on any mini-tasks will be preserved. Note: The Tiffin/DFM Computer Science course uses JavaScript as its core language. Most code examples are therefore in JavaScript. Using these slides: Green question boxes can be clicked while in Presentation mode to reveal. Slides are intentionally designed to double up as revision notes for students, while being optimised for classroom usage. The Mini-Tasks on the DFM platform are purposely ordered to correspond to these slides, giving your flexibility over your lesson structure. ?
3
Starter Can you spot any patterns in how we’re counting up? How does this pattern relate to the ‘base’? Base 10 5 6 7 8 9 10 11 12 … Base 2 1 10 11 100 … Base 3 2 10 11 12 20 21 … Base 5 344 400 401 402 … Base 2 111 1000 1001 1010 … Base 16 99 9A 9B 9C 9D 9E 9F A0 … Things you might have spotted: The maximum value for any digit is one less than the base. When we get to the maximum value for a digit, it resets back to 0, and the next digit (left of it) goes up by 1. ?
4
Introduction When we count up with ‘normal’ numbers, what is actually happening at the indicated points? 6 7 8 9 10 11 12 … 97 98 99 100 101 … ? Both digits are at their maximum so both reset back to 0. The hundreds digit goes up by 1. The maximum value for a digit is 9. So it resets to 0, and the units digit goes up by 1 because of the ‘carry’. ? Follow up question: What do you think it means for our ‘normal’ number system to be base 10? Each digit has 10 possible values. ?
5
! Base Values for each digit Base Name of number system 0 to 9 10
The base of a number system is the number of possible values for each digit. Values for each digit Base Name of number system 0 to 9 10 Denary/Decimal 0 to 1 2 Binary 0 to F (A=10, B=11, ... F=15) 16 Hexadecimal ? ? ? ? ? ? The OCR syllabus expects you to use the term ‘denary’.
6
Next number? Example of counting 1 10 11 100 101 110 111 1000 1010
We want to count in base 2 (binary). We saw the possible values for each digit are 0 to 1. 1 10 11 100 101 110 111 1000 1010 1111 Next number?
7
Counting Game! Everyone stand up. Take it in turns to count in ternary (base 3), starting at 0. If you get it wrong, you sit down. 1 2 10 11 12 20 21 22 100 101 102 110 111 112 120 121 122 200 201 202 210 211 212 220 221 222 1000 1001 1002 1010 1011 1012 1020 1021 1022 1100 1101 1102 1110 1111 1112 1120 1121 1122 1200 1201 1202 1210 1211 1212 1220 1221 1222 2000 2001 2002 2010 2011 2012 2020 Click to Skip
8
Exercise 1 What is the maximum value of each digit in base 8? Answer: 7 Find the next number is base 5: 4 b) 234 240 c) d) 344 400 A number in base 𝑛 has 𝑘 digits. How many possible values are there for each digit? 𝒏 Therefore how many possible numbers up to 𝑘 digits are there? 𝒏 𝒌 3 1 Write out the first ten numbers in each of these bases, starting at 1. Base 2: 1, 10, 11, 100, 101, 110, , 1000, 1001, 1010 Base 5: 1, 2, 3, 4, 10, 11, , 13, 14, 20 Base 4: 1, 2, 3, 10, 11, 12, , 20, 21, 22 What number comes after 555 in: Base 6? 1000 Base 7? 556 How many times does the digit 0 occur if you write out the numbers 1 to in binary? (Hint: consider all two-digit numbers, then three, and so on) 2 digit numbers: 𝟏×𝟏=𝟏 occurrence 3 digit numbers: 𝟐×𝟐=𝟒 occurrences 4 digit numbers: 𝟒×𝟑=𝟏𝟐 occurrences 5 digit numbers: 𝟖×𝟒=𝟑𝟐 occurrences 6 digit number: 𝟏𝟔×𝟓=𝟖𝟎 occurrences Total = 129 ? a ? 4 ? ? b ? ? ? c ? 5 2 ? a ? ? b ? N ? You can see the first numbers are powers of 2 and the second are going up by 1 each time. Why: Consider just 5 digit numbers. There are 2 5−1 =16 possible numbers (because the first digit has to be 1 but the other digits each have two possibilities). For each digit half of the possibilities will be 0, thus each digit (except the first) contributes 16÷2=8 zeros. There’s 4 digits which contributes zeroes, thus 8×4=32. In general for 𝑘 digit numbers, 2 𝑘−2 ×(𝑘−1) zeroes will be seen.
9
Any Base → Denary If we were to write out the digits of the denary number “2493”, what is the value of each digit? (Hint: Think primary school!) i.e. each ‘1’ in this place value is worth 1000. This subscript means the number is in base 10. We don’t include it if the base is obvious from the context. multiply ? ? ? ? = 2493 ?
10
Any Base → Denary Now suppose we had a number in base 5 instead. How do we convert it to denary? In base 10, the place values were powers of 10, i.e. 1, 10, 100, So in base 5, we’d have powers of 5, always starting with 1 on the right. ? ? ? ? ? ? ? ? = 576 ?
11
1 0 1 12 3 3 0 24 1 2 2 03 Test Your Understanding
Copy and complete in your book. ? ? = 11 = 242 ? ? ? = 51 ?
12
I’m a Mayan, Get Me Out Of Here!
The Maya numeral system is base 20 (“vigesimal”). Use the approach you used for converting other bases to denary to vote for the correct number.
13
Example = 0 3 30 60 300
14
Q1 = 126 66 126 105 156
15
Q2 123 243 53 223
16
Q3 239 144 129 1
17
Q4 = 411 121 211 111 411
18
Q5 490 1180 1980 1380
19
Q6 = 157784 582984 396884 196884
20
Exercise 2 In computing, a byte consists of 8 bits, where each bit is a binary digit. What is the largest possible number in denary that a byte can represent? 𝟐𝟓𝟓 In general, what is the largest number in denary that can be represented by 𝑛 binary digits? Give your answer in terms of 𝑛. 𝟐 𝒏 −𝟏 A three-digit number is 100 in denary. What’s the smallest the base can be? In base 4 the biggest number in denary is 𝟒 𝟑 −𝟏=𝟔𝟑, whereas in base 5 it’s 𝟓 𝟑 −𝟏=𝟏𝟐𝟒. So 5 is the smallest base. The number with digits "𝑎1𝑏", where 𝑎 and 𝑏 are unknown digits, is 107 in decimal if the number was originally in base 5, and 205 in decimal if it was originally in base 7. Determine 𝑎 and 𝑏. 𝟐𝟓𝒂+𝟓+𝒃=𝟏𝟎𝟕 𝟒𝟗𝒂+𝟕+𝒃=𝟐𝟎𝟒 Solving, 𝒂=𝟒, 𝒃=𝟐. 432 is in an unknown base, but when converted to decimal, gives 164. Determine the base. Let the base be 𝒃. Then 𝟒 𝒃 𝟐 +𝟑𝒃+𝟐=𝟏𝟔𝟒. Solving this quadratic equation gives 𝒃=𝟔. 4 Convert the following numbers from the indicated base to denary. 1 ? 11012 1112 10223 7348 2335 5306 13 7 51 35 476 68 198 ? ? 5 ? ? ? ? 6 ? ? ? What is the following Mayan number in denary? 2 N ? ? 3 When the number “a036” in base 7 is converted to denary, the value is Determine the value of the digit 𝑎. 𝟑𝟒𝟑𝒂+𝟎+𝟐𝟏+𝟔=𝟏𝟕𝟒𝟐 𝒂=𝟓 N ? ?
21
Summary So Far We have learnt that the numbers we use in everyday life are in “base 10”. But numbers can be in any ‘base’ such as base 2 (binary). The base of a number system is the number of possible values for each digit. To convert a number to denary, we just consider the value of each digit, just like in denary each digit represents “units”, “tens”, “hundreds” and so on. =𝟏𝟐𝟓+𝟏𝟎𝟎+𝟑=𝟐𝟐𝟖 ? ? ? ?
22
Denary → Any Base Do the opposite! Convert 18 from denary to binary. Remember that the value of each digit goes up in powers of the base. What is the greatest power of 2 that is less than 18? ? ? ? ? ? ? = 18 Always have the maximum digit you can. How many 16s are we allowed in making up 18? At this stage we only have 2 left to make up in order to get a sum of 18. Do we want the 8 or not?
23
2 0 4 2 5 250+ 0 + 20+ 2= 272 Convert 272 from denary to base 5.
Another Example Convert 272 from denary to base 5. Recall that the value of each digit goes up in powers of 5 (starting with 1 on the right), and we stop at the highest power of 5 that is less than 272. ? ? ? ? ? = 272 125 goes into 272 twice.
24
1 2 1 0 4 64 +32 + 4 + 0 = 100 Convert 100 from denary to base 4.
Test Your Understanding Convert 100 from denary to base 4. ? ? ? ? ? = 100
25
Exercise 3 Copy and complete the following table. 3 The denary number “7a2” is in base 5. Determine the digit 𝑎. 𝒂=𝟏 1000 is a four-digit denary number whose first digit one. In what other bases can this can be converted to such that we still have a four-digit number which starts with 1? If the base is 𝒃, 𝒃 𝟑 has to be between 500 and 1000 (if it were less, the first digit wouldn’t be 1). Only 8 and 9 satisfy this. Prove that there is no base 𝑏 such that 123 in denary can be converted to: 45 in that base. 𝟒𝒃+𝟓=𝟏𝟐𝟑 𝒃= 𝟓𝟗 𝟐 ii in that base. 𝟒 𝒃 𝟐 +𝟓𝒃+𝟔=𝟏𝟐𝟑 Solving gives 𝒃=𝟒.𝟖𝟐, −𝟔.𝟎𝟕, neither or which are integers. 1 ? Denary Binary (Base 2) Base 6 3 11 8 1000 12 10 1010 14 77 205 102 250 105 253 1365 10153 N ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? N 2 Convert 123 in denary to Mayan numerals (recall that Mayan is base 20, and that a horizontal line means 5, a dot 1 and a shell 0). ? ? ?
26
Denary → Hexadecimal One usage of hexadecimal is to represent colours.
Each colour can be composed of red, green and blue light, each of intensity varying between 0 and 255. Note to teacher: For this example, ask volunteers to explain how we can convert between the two. ...which can be represented using just 6 digits in hexadecimal, 2 for each of the three colour components. Hexadecimal is base 16, meaning there are 16 possible values for each digit. We run out of numerical symbols after 9, so we use A for 10, B for 11, C for 12, D for 13, E for 14 and F for 15.
27
Hexadecimal to Denary Convert the following from hexadecimal to denary: (a) (b) A3 Multiples of 16: 0: 0 1: 2: 3: 4: 5: 6: 7: 8: 9: A: B: C: D: E: F: Note: In an exam hexadecimal numbers are restricted to 2 digits. ? a ? b 10 3 48 +4 = 52 = 163 A means 10
28
8 12 128 +12 = 140 Convert 8C from hexadecimal to denary. 16 1
Test Your Understanding Convert 8C from hexadecimal to denary. Multiples of 16: 0: 0 1: 2: 3: 4: 5: 6: 7: 8: 9: A: B: C: D: E: F: ? = 140
29
B 7 176 + 7 = 183 16 1 Denary to Hexadecimal ? ?
We can use exactly the same method as we did for denary to any general base… Multiples of 16: Convert 183 from denary to hexadecimal. 0: 0 1: 2: 3: 4: 5: 6: 7: 8: 9: A: B: C: D: E: F: B ? ? = 183 What is the most number of 16s we can use to make up 183? The table on the left will be very handy!
30
4 E E A 64 + 14 =84 224 + 10 =234 16 1 16 1 Test Your Understanding ?
Multiples of 16: Convert 78 from denary to hexadecimal. Convert 234 from denary to hexadecimal. 0: 0 1: 2: 3: 4: 5: 6: 7: 8: 9: A: B: C: D: E: F: ? ? E E A =84 =234
31
5 10 16 1 64 32 16 8 4 2 1 Hexadecimal to Binary ? 80 + 10 = 90 ?
We already have methods to convert from denary to other bases, and other bases to denary. We could therefore convert via denary. Convert 5A from hexadecimal to binary. Convert to denary first: ? = 90 Then convert from denary to binary: ? = 90
32
Hexadecimal to Binary 5 in binary 0101 ? A = 10 in binary 1010 ?
Convert 5A from hexadecimal to binary. But a nice little trick to convert each digit separately (noting that each hexadecimal digit requires 4 bits, i.e. a nibble), and then to concatenate the results together. 5 in binary 0101 A = 10 in binary 1010 Therefore 5A in binary ? ? ?
33
Further Example ? B = 11 in binary 1011 9 in binary 1001 ?
Convert B9 from hexadecimal to binary. ? B = 11 in binary 1011 9 in binary 1001 Therefore B9 in binary ? ?
34
Test Your Understanding
Convert 6F from hexadecimal to binary. 6 in binary 0110 F in binary 1111 Therefore 6F in binary ? ? ?
35
Binary to Hexadecimal 0110 6 1101 13 D ? 6D in hexadecimal
Convert to hexadecimal We can use the same principle in reverse: Convert each 4 bits in turn: ? 6 13 D 6D in hexadecimal Convert to hexadecimal ? 12 C 1 C1 in hexadecimal
36
Adding in denary You obviously know how to add normal (denary) numbers from primary school. But we can apply the same principle to adding binary numbers. 3+9=12 We’d use the 2 then carry the 1.
37
1 0 0 1 + 1 1 0 1 1 0 1 1 0 Adding in binary 1 1 ? e ? d ? c ? b ? a
? e ? d ? c ? b ? a 1 1 Remember that in binary, = 10 (as 2 in binary is 10). So we put the 0 in this column and carry the 1.
38
Further Example ? f ? e ? d ? c ? b ? a 1 1
39
Test Your Understanding
? ?
40
Shifts Suppose we were to shift all the digits of a normal denary number 1 place to the left: Other than shift the digits, we don’t want to add anything extra, so we need to fill this out with a 0. ? Q Shifting the digits left one digit is equivalent to: Multiplying by 10. More generally, it’s equivalent to multiplying by the base. If we shifted left 2 digits, this would be equivalent to: Multiplying by 𝟏𝟎 𝟐 =𝟏𝟎𝟎 ? Q ?
41
Shifts Suppose now we were to shift all the digits of a normal denary number 1 place to the right: 1 2 6 N Note however that if we shift the digits 1 position to the right, we have to discard a digit unless we’re allowed digits after the decimal point (i.e. we had a real rather than an int).
42
Binary Shifts Carry out a 3 place left shift on the 8-bit binary number ? 1 1 Ordinarily a shift-left of 3 places would be equivalent to multiplying by 2 3 =8. However, we have an overflow error, in that the leftmost 1 has shifted off the end of the number (if we’re constrained to an 8-bit number). Carry out a 2 place right shift on the 8-bit binary number ? 1 We again pad out any blanks with 0s and discard any digits that overflow. This is equivalent to DIV 4, i.e. we divide by 4 but discard the remainder. 1
43
Test Your Understanding
Carry out a 1 place right shift on the 8-bit binary number ? Carry out a 2 place left shift on the 8-bit binary number Identify any problems encountered. ? The leading two 1s are lost due to overflow.
44
Bit Operations in JavaScript
Performs a 2 place left shift on x. e.g. 5 << 2 10 ? x << 2 7 >> 1 ? Performs a right shift. e.g. 7 >> 1 = 1112 >> 1 = 112 (last bit discarded) = 3 Not to be confused with &&. We do a logical AND on each bit in turn. 6 & 3 = 110 & 011 = 010 = 2 ? 6 & 3 ~13 ? Bitwise NOT. ~ 13 = ~ 0000… = 1111… = -14 This is because the first bit gives the sign of the number: the leading 1 indicates it’s a negative number. ? Bitwise logical OR. 6 | 3 = 110 | 011 = 111 = 7 6 | 3
45
What am I expected to know?
From the OCR Computer Science GCSE specification: This is covered elsewhere.
46
Review ? ? ? ? 1 Convert 71 from denary to binary 2
Add the binary numbers: ? ? 3 Perform a 2-place left shift on the 8-bit binary number Explain any problems encountered. 4 Convert BC from hexadecimal to binary, using 4 bits for each character. ? B 11 1011 C 12 1100 ? There is an overflow problem because the leading 1 was lost.
47
Coding Mini-Tasks Return to the DrFrostMaths site to complete the various tasks on bases.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.