Binary 1 Basic conversions
Base 10 uses the digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 represents digits as multiples of 1, 10, 100, 1000, etc… 100 10 1 594 5 9 4 500 + 90 + 4 = 594 42 2 0 + 40 + 2 = 42 420 400 + 20 + 0 = 420 402 400 + 0 + 2 = 402
Base 2 (Binary) Uses the digits 0, 1 Each column represents a “power of two” 0s and 1s are known as “bits”, short for “binary digits” 128 64 32 16 8 4 2 1 Decimal 16 + 1 = 17 64 + 16 + 2 = 82 128 + 4 = 132 128 + 64 + 8 + 1 = 201
Binary to decimal… practise! 128 64 32 16 8 4 2 1 Decimal 0010 1100 0101 1000 1010 0011 1100 0011 1111 1000 0111 0001 0101 0101 0110 0110 1111 1111
Solutions Binary 128 64 32 16 8 4 2 1 Decimal 0010 1100 32 + 8 + 4 = 44 0101 1000 64 + 16 + 8 = 88 1010 0011 128 + 32 + 2 + 1 = 163 1100 0011 128 + 64 +2 + 1 = 195 1111 1000 128 + 64 + 32 + 16 + 8 = 248 0111 0001 64 + 32 + 16 + 1 = 113 0101 0101 64 + 16 + 4 + 1 = 85 0110 0110 64 + 32 + 4 + 2 = 102 1111 1111 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255
What about going the other way? For example, how might we determine the binary equivalent of each of the following decimals? 4 16 17 15 12 39
Decimal to binary – One method Find the highest power of two that is less than the number you want to convert, then subtract, and repeat. Convert 39 into binary Does 128 fit into 39? No 0 Does 64 fit into 39? No 0 Does 32 fit into 39? Yes 1 What’s left over? 39-32 = 7 Does 16 fit into 7? No 0 Does 8 fit into 7? No 0 Does 4 fit into 7? Yes 1 What’s left over? 7-4 = 3 Does 2 fit into 3? Yes 1 What’s left over? 3-2 = 1 Does 1 fit into 1? Yes 1 What’s left over? 1-1=0 Decimal 39 => binary 0010 0111
Decimal to binary… practise! 128 64 32 16 8 4 2 1 Binary 5 19 27 35 49 87 232 240
Solutions Decimal 128 64 32 16 8 4 2 1 Binary 5 0000 0101 19 0001 0011 27 0001 1011 35 0010 0011 49 0011 0001 87 0101 0111 232 1110 1000 240 1111 0000
Binary fractions We can convert fractions in much the same way as whole numbers Binary 2 1 . ½ .5 ¼ .25 1/8 .125 1/16 .0625 1/32 .03125 1/64 .015625 Decimal 1.1 1 + 0.5 = 1.5 10.011 2 + 0.25 + 0.125 = 2.375 0.0111 0.25 + 0.125 + 0.0625 = 0.4375 11.100101 2 + 1 + 0.5 + 0.0625 + 0.015625 = 3.578125
Binary fractions What if the fractions we want to convert from decimal to binary aren’t “easy”? 0.4 0.85 Division by 2 We repeatedly divide a number by two to get the final solution
Convert decimal 0.4 to binary Multiply by 2 Put the whole number part in one column Put the remainder in the next column Copy the remainder to the start of the next row Repeat There is no rule how many times you should do this, but five “binary places” will be fine for us The “whole number” column, read from top to bottom, is the binary fraction Decimal Whole number Remainder 0.4 x 2 = 0.8 1 0.6 0.2 Therefore 0.4 => 0.0110011… 0.410 = 0.011002 (to 5 binary places)
Decimal fractions to binary… practise! Convert 0.3 to binary Convert 0.24 to binary Decimal Whole number Remainder 0.3 x 2 = Decimal Whole number Remainder 0.24 x 2 =
Data representation How could we send a secret message to someone else using only binary digits (bits)? How many bits would be needed per letter? What if we wanted to use upper and lower case letters? What about all the other symbols on the keyboard? (and don’t forget the characters that aren’t displayed, such spaces, tabs, line breaks, etc.)
ASCII ASCII Decimal Binary … ? 63 0011 1111 @ 64 0100 0000 A 65 0100 0001 B 66 0100 0010 C 67 0100 0011 D 68 0100 0100 E 69 0100 0101 F 70 0100 0110 G 71 0100 0111 H 72 0100 1001 I 73 0100 1010 J 74 0100 1011 K 75 0100 1100 “ASCII” (American Standard Code for Information Interchange) encodes 128 characters into 7-bit binary integers. Eight-bit machines typically set the eighth (left-most) bit to 0. It was originally based on the English alphabet, and includes both printable and non- printable characters. It was the most common character-encoding scheme on the web until 2007.
Unicode What if we want to represent more than just English characters? Unicode is a system for providing support for many different languages. Unicode can be implemented in different ways, but the most common are UTF-8 (8 bits) or UTF-16 (16 bits). Unicode is a superset of ASCII, the numbers 0-127 have the same meaning in ASCII as they do in Unicode. The latest definition of Unicode includes almost 100,000 characters.
ASCII characters Convert the following using the ASCII table: Decimal # 100 F 84 p 32
Solutions Character Decimal # 35 d 100 F 70 T 84 p 112 (space) 32