Binary Numbers We are used to using the decimal number system for counting and performing calculations. Nowadays, it is very rare for any other number system to be used, but you may have come across examples of other number systems such as Roman Numerals that are not decimals. Decimal Number 1 5 10 50 100 1000 1984 Roman Numeral Equivalent I V X L C M MCMLXXXIV The decimal system has been used for around 5000 years because humans find it easy to understand. Look at 1984 in the table of Roman Numerals above to see why people prefer decimal! What does decimal mean? The "dec" in decimal comes from the word "decem" in Latin which is the equivalent of the English word "ten". Decimal is named after the number 10 because: There are ten digits: 0 1 2 3 4 5 6 7 8 9 The value of a digit depends upon where it appears in a number. The place values of the digits are multiplied by ten each time you move one digit to the left. Look at the number 5633 below. The place values are written above the number. x 10 x 10 1000 100 10 1 5 6 3 3 The first three in 5633 is worth ten times as much as the second three as it is one place to the left of it. The six in the 100s column is worth 100 times as much as a six would be in the 1s column because it is two places (10*10=100) to the left of it. If you understand mathematical powers, you will see that the place values in decimal are powers of ten as 100 = 1, 101 = 10, 102 = 100, 103 = 1000 and so on. BGSB Computer Science Page 1 Binary Computers don't like decimal they like binary Human's like decimal, but computers do not. This is because computers are digital devices. It is easier, cheaper and more reliable to make computers that work using a different number system called binary. The binary number system has just two digits. They are: 01 A binary number is just a sequence of 0s and 1s, for example 01101011000101 is a binary number. A digit in binary is known as a binary digit or bit for short. Binary is called binary because "bi" means "twice" or "two" in Latin. Why do computers like binary? Computers are electronic devices that store and process data as electrical voltages. Using electronics, it is quite an easy job to represent two different values (such as the digits 0 and 1 in binary) but very hard to represent 10 different values (such as the ten digits in decimal). This is because 0 and 1 can be represented as an electrical current being off (0) or on (1). All of the electrical components inside the computer use this principle. If a computer were to use decimal then the electronics would need to be able to represent ten different values (digits 0 to 9). This could be done as different voltages. For example, the digit 0 could be 0 volts, 1 could be 1 volt and so on up until 9 being 9 volts. But creating and measuring voltages like this is much more complex than just detecting if a current is on or off, and important components inside computers like transistors on microchips cannot do this. For these reasons, there are no computers that use decimal. Place values in binary You already know that in binary there are just two digits, 0 and 1. To represent numbers with more than one digit, place values are used so that the digits at the start of a number are worth more than the digits at the end. In decimal, the place values start at one and are increased by being multiplied by 10. In binary, the place values increase by being multiplied by 2. Place values in binary are: x2 x2 128 BGSB Computer Science 64 32 16 8 Page 2 4 2 1 Binary Conversions As computers use binary and humans use decimal, it is necessary to be able to convert between binary and decimal. A computer will do this automatically for you, but if you are going to work with computers then it is also useful to be able to do this yourself. Converting from binary to decimal Converting numbers from binary to decimal is quite easy. You just need to write the place values above the digits in the binary number then add up the place values where the binary digits underneath them are 1. Example 1 Question: Convert the binary number 1101 into decimal. Answer: First, write out the place values for a four-digit binary number then write the number underneath: 8 1 4 1 2 0 1 1 Place Values Number Now, add up the place values for the columns that have a 1 in them: 8 + 4 + 1 = 13 So, the binary number 1101 is equivalent to the decimal number 13. Example 2 Question: Convert the binary number 11001011 into decimal. Answer: First, write out the place values for an eight-digit binary number then write the number underneath: 128 1 64 1 32 0 16 0 8 1 4 0 2 1 1 1 Now, add up the place values for the columns that have a 1 in them: 128 + 64 + 8 + 2 + 1 = 203 So, the binary number 11001011 is equivalent to the decimal number 203. BGSB Computer Science Page 3 Binary Questions Convert the following numbers from binary to decimal. (1) 10101 To help you answer, the place values and number have been written out for you: 16 1 8 0 4 1 2 0 1 1 Answer: ........................................................................................................................................... (2) 1011 Answer: ........................................................................................................................................... (3) 11101 Answer: ........................................................................................................................................... (4) 100101 Answer: ........................................................................................................................................... (5) 10101001 Answer: ........................................................................................................................................... BGSB Computer Science Page 4 Binary Converting from decimal to binary There are a few different ways that you can convert a number from decimal to binary. Here is an algorithm to explain one of the simplest ones, using the example of converting the denary number 181. Start at 1 at the right hand side and write out the place values for binary until the place value that you write is bigger than the number that you need to convert. For 181: 256 128 64 32 16 8 4 2 1 4 2 1 Then cross out the highest place value as you will not need this: 256 128 64 32 16 8 Next, start at the left hand end of the list of place values (128 in this case). If the place value at the current position is less than the number you have left to convert then: write a 0 under the place value move right one position. If the place value at the current position is greater than or equal to the number you have left to convert then: write a 1 under the place value subtract the place value from the number you have left to convert move right one position. Repeat the above until you have written values in under every place value. For the number 181: 128 1 64 0 BGSB Computer Science 32 1 16 1 8 0 Page 5 4 1 2 0 1 1 Binary Here are some more examples to help you understand the method. Example 1 Question: Convert the binary number 37 from decimal into binary. Answer: The place values to write out are 1, 2, 4, 8, 16, 32 and 64. 64 is then crossed out as it is bigger than 37. So, the place values to use are: 32 16 8 4 2 1 32 1 16 1 8 0 4 1 2 0 1 1 37>32 so write 1 leaves 37-32=5 5<16 so write 0 5<8 so write 0 5>4 so write 1 leaves 5-4 = 1 1<2 so write 0 1=1 so write 1 Leaves 1-1 = 0 And the conversion is: So the binary equivalent of 37 is 110101. Example 2 Question: Convert the binary number 204 from decimal into binary. Answer: The place values to write out are 1, 2, 4, 8, 16, 32, 64, 128 and 256. 256 is then crossed out as it is bigger than 204. So, the place values to use are: 128 64 32 16 8 4 2 1 And the conversion is: 128 1 64 1 32 0 16 0 8 1 4 1 2 0 1 0 204>128 so write 1 leaves 204-128 = 76 76>64 so write 1 leaves 76 - 64 = 12 12<32 so write 0 12<16 so write 0 12>8 so write 1 leaves 12-8 =4 4=4 so write 1 leaves 4-4 =0 0 left to convert so write 0 0 left to convert so write 0 So the binary equivalent of 204 is 11001100. BGSB Computer Science Page 6 Binary Questions Convert the following numbers from decimal to binary. (1) 21 To help you answer the first question, the place values have been written out for you: 16 8 4 2 1 Answer: ........................................................................................................................................... (2) 43 Answer: ........................................................................................................................................... (3) 58 Answer: ........................................................................................................................................... (4) 214 Answer: ........................................................................................................................................... (5) 706 Answer: ........................................................................................................................................... BGSB Computer Science Page 7 Binary Bits and Bytes Computers usually store numbers in fixed sized memory locations. Remember that a bit is a single binary digit i.e. a 0 or a 1. The most common size of memory location that a computer uses is a byte. A byte is 8 bits long. If a number does not need 8 bits, then if it is stored in a byte extra 0s are added to the start of it. For example, the decimal number 25 is 11001 in binary. This would be stored as 00011001 in a byte. Three extra 0s are added to the start to make it 8 bits long. Kilobytes, Megabytes and More How much storage a computer has is measured in bytes. Extra storage in the RAM will let the computer run more programs as the same time. Extra storage on the Hard Disk Drive will let you install more programs and save more files. The Manchester baby, the world's first stored-program electronic digital computer which successfully ran its first program on 21st June 1948 had 128 bytes of memory. 128 bytes of memory here! As the amount of memory a computer has increased, bytes became too small a unit to describe memory size, so other units such as Kilobytes, Megabytes and Gigabytes were invented. 1 Kilobyte (Kb) is equal to 1024 bytes. 1 Megabyte (Mb) is equal to 1024 Kilobytes. 1 Gigabyte (Gb) is equal to 1024 Megabytes. 1 Terabyte (Tb) is equal to 1024 Gigabytes. The computer that flew the rocket which landed the Apollo 11 lunar capsule on the moon in 1969 had 64 Kilobytes of memory. The Apple Macintosh Plus computer, launched in 1986, came with 1 Megabyte of memory which could be expanded up to 4 Megabytes. The Nintendo SNES console, launched in 1990, had 256 Kilobytes of memory. A modern hard disk drive can store anything from about 256 Gigabytes up to 4 Terabytes of data. BGSB Computer Science Page 8 Binary Converting Memory Sizes Sometimes it is useful to be able to convert between bytes, Megabytes, Gigabytes etc. so that you can compare the amount of memory in two different computers. Here are two example of converting memory sizes into bytes: The Apollo 11 computer had 64 Kilobytes of memory. We know that 1 Kilobyte = 1024 bytes So 64 Kilobytes = 64 * 1024 = 65,536 bytes Therefore the Apollo 11 computer had 65,536 bytes of memory. An upgraded Apple Macintosh Plus computer could have 4 Megabytes of memory. We know that 1 Megabyte = 1024 Kilobytes So 4 Megabytes = 4 * 1024 = 4,096 Kilobytes We know that 1 Kilobyte = 1024 bytes So 4,096 Kilobytes = 4096 * 1024 = 4,194,304 bytes Therefore the upgraded Apple Macintosh Plus had 4,194,304 bytes of memory. BGSB Computer Science Page 9 Binary Questions (1) How many bits are there in a byte? Answer: ........................................................................................................................................... (2) Show how the binary number 1011 would be stored in a byte. Answer: ........................................................................................................................................... (3) What is the biggest binary number that can be stored in a byte, and what is the decimal equivalent of this number? Answer: ........................................................................................................................................... (4) An image file is saved on a computer. It takes up 100 Kilobytes. How many bytes does the file take up? Answer: ........................................................................................................................................... (5) An new computer has 2 Gigabytes of memory. How many Megabytes is this equivalent to? Answer: ........................................................................................................................................... (6) Three different devices which can store data are a CD-R, a DVD-R and a USB Flash Drive. The CD-R can store 650 Megabytes of data. The DVD-R can store 4.7 Gigabytes of data. The USB Flash Drive can store 8 Gigabytes of data. Write out the names of the devices, in order, with the device that can store most data first and the device that can store least last. Answer: ........................................................................................................................................... BGSB Computer Science Page 10 Binary