Data Representation Binary Hexadecimal jamiedrfrostmaths com www drfrostmaths
Data Representation : : Binary & Hexadecimal jamie@drfrostmaths. com www. drfrostmaths. com @Dr. Frost. Maths Last modified: 7 th July 2019
www. drfrostmaths. com Registering on the Dr. Frost. Maths 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. Everything is completely free. Why not register? With Computer Science questions by: Your code on any mini-tasks will be preserved. Note: The Tiffin/DFM Computer Science course uses Java. Script as its core language. Most code examples are therefore in Java. Script. 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.
Starter Can you spot any patterns in how we’re counting up? How does this pattern relate to the ‘base’? Base 10 Base 2 5 6 7 8 9 10 11 12 … 0 1 10 11 100 … Base 3 Base 5 Base 2 Base 16 2 10 11 12 20 21 … 344 400 401 402 … 111 1000 1001 1010 … 99 9 A 9 B 9 C 9 D 9 E 9 F A 0 … 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.
Introduction When we count up with ‘normal’ numbers, what is actually happening at the indicated points? 6 7 8 9 10 11 12 … 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’. 97 98 99 100 101 … Both digits are at their maximum so both reset ? back to 0. The hundreds digit goes up by 1. Follow up question: • What do you think it means for our ‘normal’ number system to be base 10? Each digit has 10 possible values. ?
Base ! The base of a number system is the number of possible values for each digit. Values for each digit 0 to 9 0 to 1 0 to F (A=10, B=11, . . . F=15) Base 10 2 16 ? ? ? Name of number system ? Denary/Decimal Binary ? Hexadecimal ? The OCR syllabus expects you to use the term ‘denary’.
Example of counting We want to count in base 2 (binary). We saw the possible values for each digit are 0 to 1. 0 1 10 11 100 101 110 111 1000 1010 1111 Next number?
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. 0 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
Exercise 1 3 1 ? a ? b ? c ? 2 a b 4 ? ? 5 ? ? N ?
If we were to write out the digits of the denary number “ 2493”, what is the value of each digit? (Hint: Think primary school!) 1000 10 multiply i. e. each ‘ 1’ in this place value is worth 1000. 1 2 4 9 310 ? +400 ? +90 ? + 3? = 2493 ? 2000 This subscript means the number is in base 10. We don’t include it if the base is obvious from the context.
Now suppose we had a number in base 5 instead. How do we convert it to denary? ? ? 125 25 5? 1? In base 10, the place values were powers of 10, i. e. 1, 100, 1000. So in base 5, we’d have powers of 5, always starting with 1 on the right. 4 3 0 15 ? + 75 ? + 0? + 1? = 576? 500
Test Your Understanding Copy and complete in your book. 8 4 ? 2 1 1 0 1 12 8 + 0 +? 2 + 1 = 11 27 9 ? 3 64 16 ? 4 1 3 3 0 24 192 + 48 + ? 0 + 2 = 242 1 1 2 2 03 27 + 18 +? 6 + 0 = 51
t e G , n a y a M ! a e r e I’m H f O t u O e M 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.
Example 20 60 1 + 0 = 0
Q 1 20 1 + 6 = 126
Q 2
Q 3
Q 4 400 20 + 0 1 + 11 = 411
Q 5
Q 6 8000 152000 400 + 5600 20 + 180 = 1 +4
Exercise 2 1 Convert the following numbers from the indicated base to denary. 11012 1100112 10223 7348 2335 5306 2 13 7 51 35 476 68 198 ? ? ? ? What is the following Mayan number in denary? 4 ? 5 ? 6 ? N 160? 001 ? 3 N ? ?
Summary So Far ? ? ? ?
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? 16 8 ? 4 2 1 1? 0? 0? 1 0 2 ? ? 16 + 0 + 2 + 0 = 18 Always have the maximum digit you can. How many 16 s 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?
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. 125 25 ? 5 1 2? 0 4 2 ? ? ? 5 250+ 0 + 20+ 2= 272 125 goes into 272 twice.
Test Your Understanding Convert 100 from denary to base 4. ? 64 16 4 1 1? 2? 1? 0? 4 64 +32 + 4 + 0 = 100
Exercise 3 1 Copy and complete the following table. Denary 3 ? 8 10 77 102 105? 1365 2 3 Binary (Base 2) Base 6 11 3 ? 1010 ? 1001101 ? 1100110 ? 1101001 ? 1010101 ? 1000 ? 12 ? 14 ? 205? 250? N ? 253 10153 ? 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). ? ? N ? ?
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. . 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.
Hexadecimal to Denary Multiples of 16: 0: 1: 2: 3: 4: 5: 6: 7: 8: 9: A: B: C: D: E: F: 0 16 32 48 64 80 96 112 128 144 160 176 192 208 224 240 Convert the following from hexadecimal to denary: (a) 34 (b) A 3 Note: In an exam hexadecimal numbers are restricted to 2 digits. 16 3 1 16 4 10 ? 3 b ? a 48 +4 = 52 1 160 +3 = 163 A means 10
Test Your Understanding Multiples of 16: 0: 1: 2: 3: 4: 5: 6: 7: 8: 9: A: B: C: D: E: F: 0 16 32 48 64 80 96 112 128 144 160 176 192 208 224 240 Convert 8 C from hexadecimal to denary. 16 1 8 12? 128 +12 = 140
Denary to Hexadecimal Multiples of 16: 0: 1: 2: 3: 4: 5: 6: 7: 8: 9: A: B: C: D: E: F: 0 16 32 48 64 80 96 112 128 144 160 176 192 208 224 240 We can use exactly the same method as we did for denary to any general base… Convert 183 from denary to hexadecimal. 16 1 B? 7? 176 + 7 = 183 What is the most number of 16 s we can use to make up 183? The table on the left will be very handy!
Test Your Understanding Multiples of 16: 0: 1: 2: 3: 4: 5: 6: 7: 8: 9: A: B: C: D: E: F: 0 16 32 48 64 80 96 112 128 144 160 176 192 208 224 240 Convert 78 from denary to hexadecimal. 16 4 1 ? E 64 + 14 =84 Convert 234 from denary to hexadecimal. 16 E 1 ? A 224 + 10 =234
Hexadecimal to Binary We already have methods to convert from denary to other bases, and other bases to denary. We could therefore convert via denary. Convert 5 A from hexadecimal to binary. Convert to denary first: 16 1 5 10 ? 80 + 10 = 90 Then convert from denary to binary: 64 32 16 8 4 2 1 1 0 1 1 ? 0 1 0 64 +16+8 +2 = 90
Hexadecimal to Binary Convert 5 A 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 A = 10 in binary 0101 ? 1010 ? Therefore 5 A in binary 0101 1010 ?
Further Example Convert B 9 from hexadecimal to binary. B = 11 in binary 9 in binary 1011 ? 1001 ? Therefore B 9 in binary ? 10111001
Test Your Understanding Convert 6 F from hexadecimal to binary. 6 in binary F in binary 0110 ? 1111 ? Therefore 6 F in binary 01101111 ?
Binary to Hexadecimal Convert 01101101 to hexadecimal We can use the same principle in reverse: Convert each 4 bits in turn: 0110 1101 6 ? 13 D 6 D in hexadecimal Convert 11000001 to hexadecimal 1100 0001 C 1 in hexadecimal 12 ? 1 C
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. + 2 3 0 6 5 1 3 9
Adding in binary + 1 ? e 1 1 1 0 ? d 0 1 1 ? c 0 0 1 ? b 1 Remember that in binary, 1 + 1 = 10 (as 2 in binary is 10). So we put the 0 in this column and carry the 1. 1 1 0 ? a
Further Example + 1 1 0 ? f ? e 1 1 1 0 ? d 0 0 1 ? c 1 1 1 0 ? b 0 1 1 ? a
Test Your Understanding 1 + 1 0 1 1 1? 0 1 1 0 + 1 1 1 0 0 0 1? 0 1 1 0
Shifts Suppose we were to shift all the digits of a normal denary number 1 place to the left: 10000 100 10 1 1 2 6 4 0? Q ? Other than shift the digits, we don’t want to add anything extra, so we need to fill this out with a 0.
Shifts Suppose now we were to shift all the digits of a normal denary number 1 place to the right: 10000 100 10 1 1 2 6 4 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).
Binary Shifts Carry out a 3 place left shift on the 8 -bit binary number 0100 1011 0 0 1 1 0 1 ? 1 0 0 0 Carry out a 2 place right shift on the 8 -bit binary number 1011 0110 1 1 0 0 1 ? 0 0 1 1 We again pad out any blanks with 0 s and discard any digits that overflow. This is equivalent to DIV 4, i. e. we divide by 4 but discard the remainder.
Test Your Understanding Carry out a 1 place right shift on the 8 -bit binary number 1001 0110 ? 01001011 Carry out a 2 place left shift on the 8 -bit binary number 11010111. Identify any problems encountered. 01011100 ? The leading two 1 s are lost due to overflow.
Bit Operations in Java. Script x << 2 Performs a 2 place left shift on ? x. e. g. 5 << 2 10 6 & 3 Not to be confused with &&. We do a logical AND on each bit in turn. 6 & 3 ? = 110 & 011 = 010 = 2 6 | 3 Bitwise logical OR. 6 | 3 = 110 | 011 ? = 111 = 7 7 >> 1 ~13 Performs a right shift. e. g. 7 >> 1 = 1112 >> 1 ? = 112 (last bit discarded) =3 Bitwise NOT. ~ 13 = ~ 0000… 00011012 = 1111… 11100102 = -14 ? This is because the first bit gives the sign of the number: the leading 1 indicates it’s a negative number.
What am I expected to know? From the OCR Computer Science GCSE specification: This is covered elsewhere.
Review 1 3 Convert 71 from denary to binary 64 32 16 8 4 2 1 1 0 0 0 1 ? Perform a 2 -place left shift on the 8 -bit binary number 01101101. Explain any problems encountered. 0 1 1 0 1 1 0 ? 1 0 0 There is an overflow problem because the leading 1 was lost. 2 Add the binary numbers: 11010 + 1111 1 1 0 + ? 1 1 1 0 0 1 4 Convert BC from hexadecimal to binary, using 4 bits for each character. B 11 1011 C 12 1100 ? 10111100
Coding Mini-Tasks Return to the Dr. Frost. Maths site to complete the various tasks on bases.
- Slides: 47