You are on page 1of 10

Binary Representation

Taken from http://youthlinkjamaica.com/cxc/ In this week's lesson I will begin to look at binary representation and manipulation. Computers store and manipulate data numerically using the binary system (refer to as the machine language), which is comprised of '1' or '0' and, of course, we will be working with base 2 in our calculations. Converting decimal numbers to their binary equivalent To convert decimal numbers to binary you are simply going to be dividing the number by 2 and subsequently making a note of the remainder. You will stop dividing when you arrive at a zero. The binary answer is written from the bottom up. Let us now look at an example. Example 1 Convert 11 from base 10 to its binary equivalent 2 2 2 2 2 11 5 2 1 0 Reminder 1 1 0 1

Therefore 1110 = 10112 - The Base you are working with is '2' - The decimal number in base 10 - Remember you write your answer from the bottom up! Converting binary numbers to their decimal equivalent When converting binary numbers to decimal, for each of the binary digits (bit) you are going to have base 2 raised from 0 to the corresponding number of bits you have. So, if you have four bits, then base 2 will be raised from 0 to 3, for example 20 - 23. Then, the value you get when 2 is raised to the corresponding number, is multiplied by its corresponding bit starting from right to left. You then add the corresponding decimal numbers together to get the decimal equivalent of 10112. Let us look at an example. Example 2 I am going to use the answer we obtained from example one. Convert 10112 to its decimal equivalent (This way you can tell if the answer we obtained in example one is correct). 23 1 22 0 21 1 20 1

(8 x 1) + (4 x 0) + (2 x 1) + (1 x 1) = 11 Therefore 10112 = 1110 - If you are wondering how we got 20 being equal to 1. Remember anything raise to the power 0 is 1.

Binary Addition

When adding numbers in binary, there are five rules you should apply: 0+0=0 0+1=1 1+0=1 1 + 1 = 10 (This is the binary equivalent of 2, which is read as 'one zero' and not ten) 1 + 1 + 1 = 11 (This is the binary equivalent of 3, which is read as 'one one' and not eleven) Example 3 Add the binary numbers 11012 and 11112 11 1 1 0 12 + 1 1 1 12 1 1 1 0 02 - Please note for 1 + 1 the answer is written as '0' carry '1' and for 1 + 1 + 1 the answer is written as '1' carry '1'. Subtracting in Binary When subtracting numbers in binary, there are four rules you should apply: 1- 0 = 1 1-1=0 0-0=0 0-1=1 Example 4 02 1 1 0 1 12 - 0 1 1 02 1 0 1 0 12 - Please note in order to subtract 0 - 1, we had to take away one (borrow). This one we borrow becomes two (2) because we are working with base 2. Hence the answer would be '1' as indicated in the rules above. Please ensure that you include base 2 in your answers, as shown above. PRACTICE QUESTIONS 1. For questions (a) and (b), convert the decimal numbers to binary, and for (c) and (d), convert the binary digits to decimal numbers. (a) 90 (b) 25 (c) 1010002 (d) 110002 2. Add the following numbers: (a) 01012 and 00112 (b) 01112 and 00102 3. Subtract the following numbers: (a) 111012 - 00112 (b) 011012 - 00102

Lesson 2 Hello, students. This is lesson 2 and in this week's lesson we will continue to look at binary representation and manipulation. Here are the answers to the practice questions I gave you the previous lesson. 1. (a) 90 = 10110102 (b) 25 = 110012 (c) 1010002 = 40 (d) 110002 = 24 2. (a) 01012 + 00112 = 10002 (b) 01112 + 00102 = 10012 3. (a) 111012 - 00112 = 110102 (b) 011012 - 00102 = 010112 How did you do? Did you get all of them correct? Now let's learn something new? REPRESENTING POSITIVE AND NEGATIVE NUMBERS There are three systems used to represent positive and negative numbers:
y y y

Binary Coded Decimal (BCD) Sign and Magnitude Two's Complement

Binary-Coded Decimal (BCD) Converting from binary to decimal can be tedious, especially if the decimal number is large. As a result, binarycoded decimal was invented where each decimal digit in a decimal number is substituted by its four-bit equivalent binary representation. To represent a positive or negative number in BCD, a four-bit code is assigned to represent the signs '-'and '+', which are used to preface your answer. Positive (+) = 1010 (however, this is optional when writing your answer for a positive number). Negative (-) = 1011 As illustrated below, conversion from decimal to BCD is much easier than from decimal to binary: Decimal 0 1 2 3 4 5 6 7 8 9 Binary (BCD) 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001

Let's look at an example. Example 1 Given the decimal number 982, find its equivalent BCD digit.

1010 +

1001 9

1000 8

0010 2

Therefore, 982 in BCD is: 1010100110000010 (please note the bits are written all together) Example 2 Given the decimal number 264, find its equivalent BCD digit. 1011 0010 2 0110 6 0100 4

What if you are given the BCD digit to find the decimal number? How would you arrive at the answer? Here is how it is done. You group the bits in groups of four, starting from left to right or right to left, and then convert each of those grouped bits to its decimal equivalent. Example 3 Find the equivalent decimal numbers for this BCD: 001101000101 1. 0011 / 0100 / 0101 (numbers grouped in 4) 2. 0011/ 0100/ 0101 Therefore, 001101000101 = 345 Practice Questions (1) Find the BCD for the following: (a) 8897 (b) -23 (c) 4560 (2) Find the decimal numbers for the following BCD: (a) 000101011000 (b) 1011011100000101 Sign and Magnitude In this method the most significant bit position is used to store the sign of the integer. A one (1) in the most significant bit (MSB) position represents a negative number, and zero in this position represents a positive number. 1 -------> negative 0 -------> positive In this method you write the binary value, adding zeros on the left if a specific number of bits is required. Include the sign bit on the left as one of the required bits in the answer. Example 4 Convert -13 to 8-bit sign and magnitude. Note The number is negative, so the sign bit is '1'. To make it in an 8 -bit, you add zeros before the number 13 is converted to binary, which is 1101. -13 sign and magnitude is 1 (sign bit) 000 (added zeros) 1101 (13 in binary) -13 = 10001101 (please note the bits are written all together)

Lesson 3 Students, in lesson 3 of our series of lessons we will continue to look at binary representation and manipulation. Here are the answers to the practice questions which I gave you in the previous lesson on BCD. 1. (a) 8897 = 1000100010010111 or 10101000100010010111 (b) -23 = 101100100011 (c) 4560 = 0100010101100000 2. (a) 000101011000 = 158 (b) 1011011100000101 = - 705 FINDING THE ONE'S COMPLEMENT OF A NUMBER The one's complement is less used than the two's complement (which you will learn about shortly), but they are similar in nature. It just involves flipping the bits of a given number. You flip zeros to ones and ones to zeros. Ensure the number is in its positive form whether four or eight bits before you find the one's complement of the number. Example 1 Find the one's complement of 11 using 8-bits. 11 in binary is 1011 The eight bit representation of 11 would be 00001011 0 0 0 0 1 0 1 1 1 1 1 1 0 1 0 0 - These are the flipped bits where the ones became zeros and the zeros became one. FINDING THE TWO'S COMPLEMENT OF A NUMBER This is another method of representing integers. This enables subtraction to be performed by a modified form of addition, which is easier to execute in the computer. Example 2 If the number is positive or negative do the following: Step 1: Write the number in its positive sign and magnitude form Step 2: Flip the bits (find it's one complement) Step 3: Add one (1) to the number obtained in step 2 Step 4: The result is the number in its two's complement notation Example 3 Find the two's complement of -13. Step 1: 13 in positive sign and magnitude is 00001101 (if you are not sure how I arrive at this go back to the previous lesson, where we looked at sign and magnitude) Step 2: 0 1 0 1 0 1 0 1 1 0 1 0 0 1 1 0

(Bits flipped which is the one's complement of -13) Step 3:

0 +1 ______________________________ 1 1 1 1 0 0 1 1 Step 4: -13 = 11110011 (two's complement notation) CODING SCHEMES ASSOCIATED WITH DATA REPRESENTATION The combinations of 0's and 1's used to represent characters are defined by patterns, called a coding scheme. Using one type of coding scheme, the number one (1) is represented as 00110001. Two popular coding schemes are American Standard Code for Information Interchange (ASCII) and Extended Binary Coded Decimal Interchange Code (EBCDIC). ASCII is used mainly on personal computers, while EBCDIC is used primarily on mainframe computers. Example 4 Given that the ASCII code for 'k' is 1101011 find the ASCII code for the letters 'p','s' and 'g'.
y

First you determine where 'p' falls in the letters of the alphabet from the position of 'k' KLMNOP - Counting from 'L' to 'P' you will determine that 'P' comes five spaces after 'K'.

y y

Then convert the decimal number 5(the number of spaces from 'k' to 'p') to binary. 5 in binary is 1012 Add the binary equivalent of 5 to the ASCII representation of 'k' as shown below.

k -------> 1 1 0 1 0 1 1 +101 ____________________ p -------> 1 1 1 0 0 0 0 From 'p' you can now find's'
y y y

P QRS 's' falls three spaces after 'p 3 in binary is 112

p --------------> 1 1 1 0 0 0 0 +11 __________________________ s ---------------> 1 1 1 0 0 1 1 We are going to use 'k' to determine 'g'. 1. GHIJ K 2. 'g' falls 4 spaces before 'k' which means this time we will subtract. 3. 4 in binary is 1002 k ------------> 1 1 0 1 0 1 1 -100 ________________________ g ------------>1 1 0 0 1 1 1

Lesson 4 Students, this is lesson 11 of our series of lessons. In this week's lesson I will continue to look at binary representation and manipulation. PRACTICE QUESTIONS Here are two questions to recap what we did in the previous lesson. 1. Using an 8-bit binary representation, give the (i) binary representation for the decimal number 10 (ii) one's complement representation for the decimal number -10 (iii) two's complement representation for the decimal number -10 (iv) sign and magnitude representation for the decimal number-10

2. The ASCII representation of 'H' is 01001000. What is the ASCII representation of 'D'? BINARY TO OCTAL CONVERSION To convert from an integer binary number to octal you do the following: 1) First split the binary number into 3-bit sections from the Least Significant Bit (LSB) to the Most Significant Bit (MSB). 2) Convert the 3-bit binary number to its octal equivalent Please note Octal conversion is the use of base 8. Example 1 Convert the binary number 011001011010001 into its octal equivalent. Step 1 Split the binary digit 011 001 011 010 001 Step 2 Convert the 3-bit binary to octal (to do this you can use your BCD table and just remember you are simply using one bit less) 3 bit Section of Binary Number Equivalent Number Therefore, 0110010110100012 = 313218 DECIMAL TO OCTAL The conversion of a decimal number to its base 8 equivalent is done by the repeated division method (similar to what we did when we learned how to convert a decimal number to binary). You simply divide the base 10 number by 8 and extract the remainders. The first remainder will be the LSD, and the last remainder will be the MSD. Example 2 Convert 1510 to base 8. 8 8 15 1 0 Remainder 7 1 011 3 001 1 011 3 010 2 001 1

7 -------> LSD 1 -------> MSD Write out the number from MSD to LSD as shown below: 178 1510 = 178 OCTAL TO DECIMAL The procedure to do this is quite similar to what you learned when we look at converting a binary number to decimal. First, you write out the number to be converted, placing each digit under the proper position. Second, you multiply the decimal equivalent by the corresponding digit of the octal number, then add the subsequent answers. See Example below: Example 3 Convert 7438 to its decimal equivalent. 82 81 80 3

7 4 (64 x 7) + (8 x 4) + (1 x 3) = 48310

Please note that in your conversion it goes up to 87 and then you skip to 810 should you be asked to convert a big number form base 8 to base 10. Base 8 does not include itself (88) and nine (89). Practice Questions 1. Convert this binary digit: 011000100010 to base 8. 2. Convert 26410 to octal. 3. Convert 265258 to its decimal equivalent. Let us now look at how the hexadecimal system works. We will start looking at it this week but we will conclude looking at it with subsequent examples next week. HEXADECIMAL SYSTEM The hexadecimal system uses 16 digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, and thus the base is 16. Hexadecimal numbers are compact and easy to read. It is very easy to convert numbers from the binary system to a hexadecimal digit using this table: Decimal (Base 10) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Lesson 5 Binary (Base 2) 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 Hexadecimal (Base 16) 0 1 2 3 4 5 6 7 8 9 A B C D E F

Students, this is lesson 12 of our series of Gleaner IT lessons. This week I will conclude looking at binary representation and manipulation. Here are the solutions to the practice questions you were given in the previous lesson. 2 2 2 2 10 5 2 1 0 1010 = 10102 10 in its 8-bit binary representation = 000010102 (ii) The ones complement representation for the decimal number negative 10 is: 11110101 (we changed all ones to zeros and all the zeroes to ones) (iii) The twos complement representation for the decimal number negative 10 is: 11110101 _____+ 1 11110110 (iv) Sign and magnitude representation for the decimal number negavtive 10 is: 10001010 2. The ASCII representation of 'H' is 01001000. What is the ASCII representation of 'D'? DEFGH D falls 4 spaces before H so we will subtract. 4 = 1002 H 01001000 _____ - 100 D 01000100 3. 011/ 000/ 100/ 010 to base 8 = 30428 4. 8 8 8 8 264 33 4 0 Remainder 0 1 4 Remainder 0 1 0 1

26410 = 4108 5. 84 2 83 6 82 5 81 2 80 5

(2 x 4096) + (6 x 512) + (5 x 64) + (2 x 8) + (5 x 1) = (8192) + (3072) + (320) + (16) + (5) 265258 = 1160510

HEXADECIMAL SYSTEM

Converting a hexadecimal number to decimal, the procedure to do this is quite similar to what you learned when we look at converting a binary or octal number to decimal. First, you write out the number to be converted, placing each digit under the proper position. Second, you multiply the decimal equivalent by the corresponding digit of the hexadecimal number, and then add the subsequent answers. See example below: Example 1 Convert the number 1128 hexadecimal to decimal. 16 1
3

16 1

16 2

16 8

(1 x 4096) + (1 x 256) + (2 x 16) + (8 x 1) (4096) + (256) + (32) + (8) = 4392 112816 = 439210 Example 2 Convert the number FA8 hexadecimal to base 10. 162 F 161 A 160 8

( 15 x 256) + ( 10 x 16) + (8 x 1) - Remember that hex F = decimal number 15 - Remember that hex A = decimal number 10 You were provided with a table last week to assist you with such a conversion. Converting from decimal to hexadecimal In order to convert from decimal to hexadecimal, it is required that you divide the decimal value by 16. Each time you should remember the result and keep the remainder, the divide process continues until the result is zero. The remainders are then used to represent a value in that system. Let's us now look at an example. Example 3 Convert the value 3910 to base 16 (hexadecimal). 16 16 39 2 0 Remainder 7 2

- You answer from the bottom up. 3910 = 2716 Example 4 Convert the decimal number 4386810 to base 16 16 16 16 16 43868 2741 171 10 0 Remainder 12 5 11 10

- You answer from the bottom up. 4386810 = AB5C16 (Remember to use the conversion table to assist you if you forget)

You might also like