1 CHAPTER 3 Number System and Codes 3.1 Introduction On hearing the word number, we immediately think of familiar decimal number system with its 10 digits; 0,1, 2,3,4,5,6, 7, 8 and 9. these numbers are called Arabic. Our present number system provides modern mathematicians and scientists with a great advantage over those of previous civilizations and is an important factor in our advancement. Since hands are the most convenient tools nature has provided human being have always tended to use them in counting. So the decimal number system followed naturally from this usage. The key feature that distinguishes one number system from another is the number system's base or radix. This base indicates the number of digits that will be used. To determine the quantity that the number represents, it is necessary to multiply each digit by an integer power of r, and then form the sum of all weighed digits. It is possible to use any whole number greater than one as a base in building a numeration system. There are four systems of arithmetic which are often used in digital system. These are: Decimal Binary Hexadecimal Octal 3.2 Decimal and Binary Numbers The decimal system of counting and keeping track of items was first created by Hindu mathematicians in India in A.D Since it involved the use of fingers and thumbs, it was natural that this system would have 10 digits. The system found its way to all the Arab countries by A.D. 800, where it was named the Arabic number system, and from there it was eventually adopted by nearly all the European countries by A.D. 1200, where it was called the decimal number system. The key feature that distinguishes one number system from another is the number system's base or radix. This base indicates the number of digits that will be used. The decimal number system, for example, is a base 10 number system which means that it uses 10 digits (0 to 9) to communicate information about an amount. A subscript is something included after a number when different number systems are being used to indicate the base of the number. For example would represent a number in base 7 (using the digits 0 to 6) number system. Example and are two numbers which contain the same digits but which are written using different bases. The first number we recognize as one thousand one hundred in the decimal (base 10) number system. The second number is a number in the base 2 number system called the binary number system. We shall look at the binary number system later.
2 26 CHAPTER 3 Decimal Numbers Written Using Positional Notation Consider the decimal number Each digit in this number has a positional value. The last digit 7 is the units digit. Its positional value is 7 x 1. The 3 is the tens digit. Its positional value is 3 x 10. The 2 is the hundreds digit. Its positional value is 2 x 100. The 5 is the thousands digits. Its positional value is 5 x We may write then that: = 5 x x x x 1 using positional value notation. The position, then, of each digit of a decimal number determines the weight of that digit. The digit which contributes the least weight is called the LSD (least significant digit) and the digit which contributes the most weight is called the MSD. In our example, 7 is the LSD and 5 is the MSD. In chart form, we can write: Positional Name thousands hundreds tens units Positional Weight Value in Positional Notation Problem Write using positional value notation x x x x The Binary Number System Counting in the Decimal (Base 10) Number System You know how to count in the decimal system: Start Counting: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. When you reach 9, you run out of units digits to use. So you reset the units digit to 0 and carry 1 to the tens column. Then you can keep counting. Continue Counting: 10, 11, 12,..., 19. When you reach 19, you must reset the units digit to 0 again and carry 1 over to the tens column which when added to the one there gives 2 and you may continue counting. Count some More: 20, 21, 22, 23,..., all the way to 98, 99 When you reach 99, you have to reset the units column to 0 and carry 1 which will cause the tens column to have to be reset to 0 and 1 carried over to the hundreds column. Continue Counting: 100, 101, 102, and... well, Counting in the Binary (Base 2) Number System
3 Number System and Codes 27 The binary system is a base two system. The only digits that can be used are 0 and 1. Let's start counting: Start counting: 0, 1 We have used both 0 and 1 for the units digit. That's it for the units digit. The units digit must now be reset to 0 and a 1 carried to the next digit. Continue counting: 10, 11 Now the units digit has to be reset again to 0 and a 1 carried to the left. This will cause the second digit from the right to have to be reset making the third digit from the left now 1. Keep Counting: The next 8 binary numbers are: 100, 101, 110, 111, 1000, 1001,1010, 1011 Keep your finger off the scroll bar and see if you can list the next four binary numbers, then check the table below: The first 17 decimal integers and their binary equivalents: Decimal Binary Decimal Binary Positional Notation in the Binary Number System We saw that each digit in a decimal number carries its own weight. The rightmost digit in a decimal number is the units digit; the weight of each digit increases by a factor of 10 as one moves to the left. Computers use binary number notation extensively. In computers, each digit in a binary number is called a bit. Eight bits make up one byte. The rightmost digit in a binary number is the units digit. The weight of each digit increases by a factor of 2 as one moves from right to left in a binary number. The following table gives the positional weight of each bit in the byte and uses the positional weight of each column to express the number in decimal form: Positional Weight Value Number Positional Notation = 1x x64 + 0x32 + 0x16 + 0x8 + 1x4 + 0x2 + 1x1 = 197 Note the LSD in is the right most digit which is 1 and the MSD is the left most digit which is 1. Problems Convert to decimal numbers: a) b)
4 28 CHAPTER Converting Decimal Numbers to Binary Numbers Method A - Write the Number as a Sum of Powers of 2 Any decimal number between 1 and 8 can be quickly converted into a binary number between 0 2 and by writing the number as the sum of powers of 2. Example Convert to binary by writing 11 as a sum of powers of 2 Solution: The powers of 2 are: 1, 2, 4, 8, 16, 32 etc. Powers of = = Any decimal number between 8 and 128 can also be converted to binary reasonably easily using this method: Example Convert to binary: Powers of = = Method B - Use Repeated Division To convert a decimal number to binary, we can also use repeated division and note the remainder after each division. Example Convert to binary converted to a binary number: Division Remainder 1 LSD MSD
5 Number System and Codes 29 Note that the first remainder gives the LSD and the last remainder the MSD. Then, = Check: = = 837 Problems Convert to binary numbers using Method A expressing the number as a sum of powers of 2: a) b) Convert to binary number using Method B -repeated division by 2 a) b) Binary Numbers and Computers Binary is the language of computers. Everything you type, input, output, send, retrieve, draw or paint is, in the end, converted to the computer's native language, binary. Each byte of memory consists of 8 bits. Each bit is a binary 1 or 0. When you type the letter A, the decimal number 65 is stored in the computer's memory in a byte of memory in binary form as The table below gives ASCII (American Standard Code for Information Interchange) codes for the letters A through I. Letter Decimal Representation 7 bit Binary Representation A B C D E F G H I Suppose your IBM ThinkPad has 9.76 gigabytes of memory. A gigabyte is roughly 1 billion bytes, so you could store roughly 10 billion letters in your computer's memory. That's 80 billion 0's and 1's. A page of word processing contains about characters. That means you could store 3.5 million pages of information in your computer's memory, again all in 0's and 1's. For Discussion: In a computer, registers are used to store information. An 8-bit register is one which can hold up to 8 digits. Many computers work with 8-bit registers or multiples of 8-bit registers (that is 16, 24, 32, etc). a) How many different binary representations can be stored in an 8-bit register? b) Repeat a) for a 16 bit register c) Generalize to come up with a rule: If you have an x-bit register, how many different binary representations can be stored in the register?
6 30 CHAPTER 3 d) What is the smallest and largest decimal number that can be stored in an 8-bit register? in a 16-bit register? Problem Assignment Give the next three binary numbers after the following binary numbers: a) 10 2 b) c) d) e) Convert the following binary numbers to numbers in base 10: a) b) c) d) Use a sum to convert the following decimal numbers to binary numbers: a) b) c) d) Convert the following decimal numbers to binary using division: a) b) What are the full names for the acronyms LSD sand MSD. Answers Problem x x x 1 Problems a) 8 b) 13 Problems a) b) a) b) Problem Assignment a) 10, 11, 100, 101 b) 100, 101, 110, 111 c) 110, 111, 1000, 1001 d) 1010, 1011, 1100, 1101 e) 1111, , , a) 11 b) 9 c) 13 d) a) b) c) d) a) b) LSD =least significant digit MSD = most significant digit 3.4 Hexadecimal Numbers In the previous section we looked at the decimal (base 10) and binary (base 2) number systems. In this section we look at the hexadecimal (base 16) number system. Both binary and hexadecimal numbers are used in computing. The decimal (base 10) number system uses 10 digits: 0, 1, 2, 3,..., 9 The binary (base 2) number system uses 2 digits: 0, 1 The hexadecimal (base 16) number system uses 16 digits: 0, 1, 2, 3,..., 9, A, B, C, D, E and F. The first 10 whole numbers in the hexadecimal system are: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. Once we reach 9, we run out of ordinary digits, since there are only ten digits available. Hexadecimal uses the following convention: The number = A 16, = B 16, = C 16, = D 16, = E 16, = F 16. So the first sixteen whole numbers in the hexadecimal number system are: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E and F. After we reach the number F there are no more digits left to use for the units digit. The next number is 10. Continuing to count we have: 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 1A, 1B, 1C, 1D, 1E, 1F, 20,..., 2F, 30,..., 3F
7 Number System and Codes 31 Problems Can you count in hexadecimal? a) 45, 46, 47, 48,... b) 116, 117, 118,.. c) 3A7, 3A8, 3A9,... d) AB, AC, AD,... e) A997, A998, A999,... Give the next five numbers in each of these sequences: Positional Weights of Digits in the Hexadecimal System - Conversions Hex to Decimal and Decimal to Hex The hexadecimal column weights are: Example The hexadecimal number 13A7 16 written using positional notation is: 13A7 16 = 1 x x A x x 16 0 To convert a hexadecimal number to a decimal number, we evaluate the expression for the number using positional notation. Example The decimal value of 13A7 16 can be determined by evaluating the positional notation expression. 13A7 16 = 1 x x A x x 16 0 = 1 x x x x 1 = Problem Show that 573A 16 = Problems Convert the following hexadecimal numbers to decimal numbers: a) 8C5 16 b) AEF Converting Decimal Numbers to Hexadecimal Numbers As we did for converting a decimal number to a binary number, to convert a decimal number to hexadecimal number, we shall use repeated division and note the remainder after each division. There were only two possible remainders 0 and 1 when converting to binary. In converting to base 16, however, there are 16 possible remainders is converted to hexadecimal in the table below. This method is called the calculator method converted to a hexadecimal number: Division Remainder x 16 = 11 B LSD x 16 = 11 B x 16 = 15 F 1 < 16 end the algorithm 1 1 MSD Therefore, = 1FBB 16.
8 32 CHAPTER 3 The conversion can be checked by converting 1FBB 16 back to a decimal number. 1FBB = 1 x x x = Problem Convert into a hexadecimal number and check Converting Binary Numbers to Hexadecimal Numbers As they increase in magnitude, binary numbers quickly become unwieldy for those involved with their use in computers. The five digit decimal number in binary is It can be seen that a 0 or 1 can easily be dropped or added. It is easy to convert binary numbers to their equivalent hexadecimal form. For this reason, binary numbers when used with computers are often written in hexadecimal form. Example To convert a binary number such as above to hexadecimal, convert each group of 4 digits beginning with the LSD into hex. Binary Number Grouped into 4's Equivalent Hex Digit It follows then that the = Problem Show that = 2B Converting Hexadecimal Numbers to Binary Numbers To convert a hexadecimal number to a binary number, we reverse the above procedure. Example To convert the hexadecimal number 9F2 16 to binary, each hex digit is converted into binary form. 9F2 16 = Problems a) Convert hexadecimal 2BF9 to its binary equivalent. b) Convert binary to its hexadecimal equivalent Hexadecimal Numbers in Computing Although numbers in a computer are stored in binary form, the hex representation is usually used. The hex representation of a byte requires only two digits and the hex representation of two bytes (a word) requires only 4 digits in hex compared to 16 digits in binary. The table below gives the decimal and hex representation for all the ASCII codes from 0 to Problem Assignment What is the base of each of the following number systems: a) decimal number system b) binary number system c) hexadecimal number system? 2. What are the decimal and hexadecimal equivalents of ? 3. Convert to its hexadecimal equivalent. 4. What are the binary and hexadecimal equivalents of 43 10?
9 Number System and Codes 33 Answers Problems a) 49, 4A, 4B, 4C, 4D,... b) 119, 11A, 11B, 11C, 11D.. c) 3AA, 3AB, 3AC, 3AD, 3AE.. d) AE, AF, B0, B1, B2. e) A99A, A99B, A99C, A99D, A99E... Problems a) 8C5 16 = b) AEF1 16 = Problem = Problems a) 2BF9 16 = b) = CE1 16 Problem Assignment a) 10 b) 2 c) , 1D BF7A , 2B 16
10 34 CHAPTER Binary Arithmetic Binary Addition When two single digit decimal numbers are added, there are many possible sums: Examples: = 9, = 15, = 12. When two decimal numbers containing more than single digits are added, the method of carrying digits from one column to another is used. Example: To arrive at an answer of 152, one adds the 7 and 5 to get 12. The 1 from the 12 is carried to the tens column and = 15. We saw in an earlier section that when you use the binary number system, only the digits 0 and 1 are used. Following are some examples of adding binary digits: First Addition: Add two zeroes: = 0 Second Addition: Add a 0 and a 1: = 1 or = 1 Third Addition: Add two 1's: = 10 Fourth Addition: Add two 1's and a 1 which was carried over from the previous column: = 11 Example Add these binary numbers: [We follow the usual convention of leaving a space between groups of 4 binary digits] If this addition is analyzed column by column: Column 5 Column 4 Column 3 Column 2 Column 1 First Number Second Number Sum = 1 1 (include the 1 carried from the previous column to the right) = 10 1 is carried left to the next column = = 1
11 Number System and Codes Problem Assignment Perform the following additions of binary numbers: a) 1 +1 b) 1 +0 c) d) e) f) g) h) i) j) Add: (check by converting to decimal) 3. Add +18 and +12 in binary and convert back to decimal. 4. Add +30 and +34 in binary and convert back to decimal. Answers 1. a) 10 b) 1 c) 11 d) 100 e) 101 f) 110 g) 1001 h) 1011 i) j) = = Storing Integers in Binary - Sign-Magnitude Representation As we have seen in previous sections, information is stored in computers in binary form. Traditionally information is stored in bytes consisting of 8 bits or in words consisting of 2 bytes (16 bits) or double words consisting of 4 bytes or 32 bits. The meaning of a "word" is computers can vary from machine to machine. In larger mainframes a word is defined as 64 bits. Another term that is used is nibble (or nibble). A nibble is half a byte or four bits. Example For example, the letter A is stored in a computer in ASCII code form as the number 65 which stored in a byte in binary is Example If a byte contains the bits , the decimal representation is = 123. The character having ASCII code 123 is the left bracket "(". We also discussed earlier than when working with binary numbers, people involved with computers often use hex notation rather than binary notation. Problem Write and in hexadecimal form.
12 36 CHAPTER 3 Labeling of Bits In Example above we indicated that the ASCII code for the letter A is the decimal number 65. In binary, this number is The bits in a byte are labeled from 0 (the least significant bit) to 7 (the most significant bit) Bit Number Binary Representation of the letter A MSB Current computers also use 16 bit, 32 bit and even 64 bit "words" to store information. However, at this point we shall restrict ourselves to dealing with looking at information stored in 4 bits (a nibble) or in bytes Representing Integers in Binary ASCII codes are pure or true binary numbers. By that we mean there is no positive or negative sign attached to these numbers. We are now going to look at representing integers in binary. For example, points on a grid are represented using ordered pairs. You may recall from high school math that the ordered pair P(-4,2) represents a point in the second quadrant of an xy-plane. To store this ordered pair would require the storage of the negative integer -4. In other example, we want to add +17 and -11 on a computer to yield the result +6. The integers +17 and -11 would be stored somewhere in the computer's memory. To be added, they would both have to be brought into the central processing unit (CPU) of the computer and put into registers where they would be added. Here are some of the challenges we face when we move to representing integers in binary: 1. Integers can be positive or negative. Any representation of an integer must have the sign as part of the representation of the number. 2. Each integer should have a unique representation. 3. We added two binary numbers in the last section. Once we represent positive and negative integers in binary form, we want to be able to add and subtract them. 4. The addition and subtraction should be as efficient as possible. We would like to perform these operations using the smallest number of circuits possible. LSB Sign-Magnitude Binary Number System The sign and magnitude number system is a simple binary code system used to represent positive and negative integers. In this system, the first bit (the MSB) in a binary representation is a sign bit (0 for positive and 1 for negative) followed by the magnitude bits. Example If information is stored in nibbles using sign-magnitude notation, 0101 represents a positive integer because the MSB is 0 while 1010 represents a negative integer because the MSB is 1. In particular situations, the number of bits is specified and if not all bits are used, they are filled with zeroes.
13 Number System and Codes Representation of Positive Integers using Sign-Magnitude Notation Example When a 4-bit sign-magnitude convention for representing numbers is used, the number 0101 represents the number +5. The most significant bit (MSB) is the first bit which is a 0. The 0 indicates the number is positive. The remaining 3 bits represent the magnitude of the number. The decimal value of binary 101 is = 5. Therefore, 0101, using a 4-bit sign-magnitude convention is the integer +5. The following table lists all the positive numbers that can be represented using 4 bits: Decimal Number Binary Representation Using 4 bit sign-magnitude notation Problem How many positive binary numbers can be represented in a byte using the signmagnitude notation? Representation of Negative Integers using Sign-Magnitude Notation Example When an 8-bit sign-magnitude convention for representing numbers is used, the number represents the negative integer -55. The first bit, the MSB, is 1 indicating the integer is negative. The next 7 bits represent the magnitude of the integer. Binary has decimal value = 55. Example Store -5 using a 4-bit sign-magnitude convention. Solution: -5 is stored as 1101 The first digit, the MSB, is 1 because the number is negative. The last three digits 101 is 5 in binary. 3.8 Problem Assignment List all of the negative integers that can be stored in a nibble (4-bits) in binary using the sign-magnitude notation. 2. How many different negative integers can be stored in a byte using the signmagnitude notation?
14 38 CHAPTER 3 3. What are the decimal values of these bytes storing sign-magnitude integers? Write '-93' in sign-magnitude representation, using an 8-bit register. 5. What is the largest positive number which can be represented in an 8-bit register, using the sign-magnitude method of representation? 6. What is the largest negative number which can be represented in an 8-bit register, using the sign-magnitude method of representation? 7. Give the range of possible numbers which can be represented in a 16-bit register, using the sign-magnitude method. 8. A drawback to the sign-magnitude notation is that there are two representations of the integer 0. What are they? 9. We now have two ways of representing integers in binary: true binary form and sign-magnitude notation. Complete the following table: A byte contains the following bits a) b) c) d) What integer does this byte represent if the bits represent an integer in true binary form? What integer does this byte represent if the bits represent an integer using sign-magnitude notation? 10. Complete the following table: A decimal number is to be stored in a byte. a) +11 Give the representation if true binary form is used. Give the representation if the sign-magnitude form is used? b) +147 c) -17 d) -57 As indicated in the above problems, a drawback to using the sign-magnitude notation to represent integers is that there are two representations of the integer 0. In computers, equality of numbers is usually determined by checking bit by bit for equality. There would have to be a special algorithm for equality just to deal with the integer 0. The sign-magnitude notation is used to store integers in computers. For example, signmagnitude notation would be appropriate to store the components of the ordered pair (- 5,7) if this ordered pair represents a point on a plane. However, there is another way of storing positive and negative integers that proves to be a representation that makes addition and subtraction of integers easy and efficient. This notation will be discussed in the next section.
15 Number System and Codes 39 Answers Problem B Problem If 0 is counted as a positive integer, the numbers range from to which is 0 to 127 which is 128 different integers Problem Assignment Binary Decimal Value if 0 is included ( ) ( ) 7. from to and A byte contains the following bits What integer does this byte represent if the bits represent an integer in pure binary form? a) What integer does this byte represent if the bits represent an integer using sign-magnitude notation? b) c) d) A decimal number is to be stored in a byte. Give the representation if true binary form is used. Give the representation if the signmagnitude form is used? a) b) cannot be represented using a byte c) -17 d) -57 no true binary representation in computers -- must use a sign bit no true binary representation in computers
16 40 CHAPTER One's Complement Notation We saw in previous sections that there are different ways of storing binary numbers in computers. A number, such as decimal 84, may be stored in true binary form in a byte as When this same number 84 is thought of as the integer +84, then the sign must be stored as part of the representation. The convention we used to store this positive integer in a byte was the sign-magnitude notation. The sign-magnitude representation of +84 in a byte, , is the same as the representation of 84 in true binary form in a byte. Although the representations are the same, the MSB of 0, which is bit 7 has a different interpretation in each notation. When the number 84 is stored without a sign in true binary form as in a byte, the MSB of 0 is part of the magnitude of the number. When the positive integer +84 is stored in a byte, using sign-magnitude notation, as , the MSB of 0 indicates that the integer is positive. The magnitude of the integer is determined from the remaining bits 0 to 6. We have also stored negative integers, such as -47, in a byte using sign-magnitude notation. Can you explain why the sign-magnitude representation of -47 stored in a byte using signmagnitude notation is ? We also saw that there are two representations of the integer 0 using sign-magnitude notation. Using bytes, these representations are and We also commented that it is not easy to perform both addition and subtraction electronically using integers stored in bytes in either true binary or sign-magnitude notation. Therefore, in this section we look at another way of storing integers in computers. This method is called the 1's complement notation. We introduce this notation in this section so that in the next section we can determine the 2's complement of an integer and from there go on to add and subtract integers The 1's Complement in Binary of a Positive Integer The 1's complement representation in binary of a positive integer is no different from the sign-magnitude representation of that integer. Example Using a byte, the 1's complement in binary of +84 is Problems Using a byte, what is the 1's complement in binary of +73? 2. Using a byte, what is the 1's complement in binary of +23?
17 Number System and Codes The 1's Complement in Binary of a Negative Integer The determination of the 1's complement in binary of a negative integer is not quite so straightforward as calculating the 1's complement in binary of a positive integer. The formal way to determine the 1's complement in binary of a negative integer uses the following rule: Rule: The 1's complement in binary of a negative integer is obtained by subtracting its magnitude from 2 n -1 where n is the number of bits used to store the integer in binary. This is best illustrated using an example. From this point on we replace 1's complement in binary with 1's complement. We assume that we are working in binary. Example Store the integer -36 in a byte in 1's complement form. Step 1: = [convert the magnitude of the integer to binary] Step 2: A byte contains 8 bits. Therefore, subtract from = 255. In binary 255 is This determination of the 1's complement of a negative integer would seem to be awkward and one would imagine that the computer circuitry needed to accomplish finding the 1's complement would be complex. However, we note the following: Using a byte, the 1's complement of +36 is Using a byte, the 1's complement of -36 is If these representations are compared bit by bit, one can see that corresponding bits are inverted. There is in fact, then, an easy way to determine the 1's complement of a negative integer. Determine the 1's complement of the corresponding positive integer and invert all bits. Example Using a byte, find the 1's complement of -57. Solution: +57 stored in a byte in 1's complement form is Invert all bits to determine the 1's complement of -57 to be Problems Determine the 1's complement, in byte form, of the decimal integers a) -10 b) -45 c) -111
18 42 CHAPTER 3 2. Using a nibble, determine the 1's complement, of the decimal integers a) -6 b) -28 The 1's complement notation was popular in early digital systems. Integers in these early digital systems were stored in 1's complement form. There are still two representations of the integer 0 (can you give them?) and both addition and subtraction cannot be performed easily Converting from 1's Complement Notation to Decimal Notation We look only at integers stored in bytes in the following discussion. The same rules apply if an integer is stored in 4-bits, 16-bits, 32-bits or other bit form. Case 1: The MSB of the Byte is 0 If a byte contains, for example, which represents an integer stored in 1's complement form, we know that since the MSB = 0, this is a positive integer. The decimal integer value of the byte then is : This 0 indicates the integer is positive Therefore which represent a binary number in 1's complement form is the decimal integer = +89 Case 2: The MSB of the Byte is 1 Method A - Reverse the Steps Carried out When Converting to 1's Complement Form Example In Example above we determined that -57 stored in a byte in 1's complement form is If you are told that a byte contains these same bits and that this information represents a decimal integer in 1's complement form, how would you determine that this represents the integer -57? Solution: If you know that the bits represent an integer in 1's complement form, first examine the MSB which is 1. This indicates the integer is negative. You inverted bits to find the 1's complement. Therefore, invert again and then take the magnitude of the resulting binary expression to find the decimal integer vale. When you invert all bits of you get The corresponding decimal integer is = +57. Therefore, the integer stored is -57. Method B - Add the Weight of all Bits Containing a 0
19 Number System and Codes 43 Time saving methods is always being sought in the computer industry. They can, indeed, be worth much money. In converting from 1's complement notation to decimal value notation in Method A above, we carried out three steps: Step 1: Examine the MSB. If it is 1, the integer is negative. Step 2: Invert each bit of the integer in 1's complement form. Step 3: Convert all bits (excluding the MSB) to corresponding decimal form. Steps 2 and 3 can be combined by adding the weight of all bits in the original representation containing a 0. In our example above, to convert to decimal, the MSB has value 1. Therefore, the integer is negative. Bits 5, 4, 3 and 0 have value 0. Therefore, the integer value is - ( ) = -57. These steps are summarized in the following table: Binary Number in 1's Complement Notation Weight of Bits Containing 0's This is the sign bit indicating the integer is negative Therefore which represents a binary number stored in 1's complement notation has decimal value -( ) = -57 Problems The following bytes represent integers in 1's complement form. Find their decimal values: a) b) c) d)
20 44 CHAPTER Problem Assignment Write all of the different representations of numbers in binary that can be stored in 1's complement form in a nibble and their corresponding decimal integer values. 1's Complement Representation Decimal Integer We now have three ways of representing integers in binary: true binary form, signmagnitude notation, and 1's complement notation. Complete the following table: A byte contains the following bits a) b) c) d) What integer does this byte represent if the bits represent an integer in pure binary form? What integer does this byte represent if the bits represent an integer using sign-magnitude notation?. 3. Store +6 in 1's complement form using 4 bits. 4. Store -7 in 1's complement form using 4 bits. 5. Store +83 in 1's complement form in a byte. What integer does this byte represent if the bits represent an integer in 1's complement notation?
21 6. Store -107 in 1's complement form in a byte. Number System and Codes Convert the following numbers stored in 1's complement form in a byte to decimal: a) b) Express the decimal number -39 as an 8-bit number in sign-magnitude and 1's complement form. Answers Problems Problems a) -10 b) -45 c) a) 1001 b) +28 in binary is This number cannot be stored in a nibble. Problems a) b) c) d) Two's Complement Notation We saw in previous sections that there are different ways of storing binary numbers in computers. The number 50 can be stored in a byte in true binary form as When the sign of the number is relevant, as in a number such as +50, we can store it in a byte using sign-magnitude notation as The MSB, which has value 0, indicates the number is positive. The number -50 is stored in a byte using sign-magnitude notation as The MSB, which has value 1, indicates the number is negative. The number +50 can also be stored in a byte in 1's complement form as The number -50 is stored in a byte in 1's complement form as Problems Complete the following table: Integer to be Stored in a Byte n/a True Binary Representation Two's Complement Notation Sign-Magnitude Representation 1's Complement Representation The last notation that we look at is called the two s complement notation. The 2's complement notation is used almost exclusively in digital systems to store positive and negative integers. We look then at writing integers in binary in 2's complement form.
22 46 CHAPTER The 2's Complement in Binary of a Positive Integer As was the case with the 1's complement, the 2's complement of a positive number is no different from the representation of that number using sign-magnitude notation. Example Store the integer +27 in a byte using sign-magnitude form and using the two's complement form. +27 is stored exactly the same using either the sign-magnitude notation or the 2's complement notation The number +27 is stored as: The MSB is 0 since the number is positive is 27 in binary. Example What is the decimal integer value of stored in a byte if the representation is: a) sign-magnitude form b) 2's complement form? Solution: There is no difference between the forms. Since the MSB is 0, the number is positive. The other 7 bytes in decimal form is 89. The integer stored is +89. Problems Store these integers in bytes using the 2's complement notation: a) + 36 b) Storing Negative Integers in Bytes Using Two's Complement Notation We indicated in the last section that the formal way to determine the 1's complement of a negative integer to be stored in binary in n bits is to subtract the integer in binary from 2 n - 1. It turned out that the short way to do this is to simply invert all the bits of the representation of the corresponding positive integer. Example To find the 1's complement of -77 we note that +77 in binary is Inverting bits, we determine that the representation of -77 in binary in 1's complement form is The formal way to determine the 2's complement of a negative integer to be stored in binary in n bits is to subtract the integer in binary from 2 n. To determine 1's complement of a negative integer to be stored in n bits you subtract from 2 n 1 and to find the 2 s complement of a negative integer to be stored in n bits you subtract from 2 n. It follows that the 2's complement is just 1 more than the 1's complement. This gives us a method to determine quickly the 2's complement of a negative integer. Rule: To determine the 2's complement of a negative integer, determine the 1's complement and add 1.
23 Example Store -27 in a byte using 2's complement notation. Steps 1 and 2 determine the 1's complement: Step 1: +27 in binary is Number System and Codes 47 Step 2: Invert bits to yield Then the complement of -27 is Step 3 is the additional step needed to find the 2's complement. Step 3: Add 1 to the 1's complement: The 2's complement of -27 is Example Store the integer -70 in a byte using the two's complement notation. The steps are carried out in the table that follows: Step Procedure Binary Answer in Byte Form 1 2 Write +70 in binary 70 = Take the one's complement (invert the contents of the byte) Add 1 to the LSB + 1 The answer is the two's complement of Therefore we store the integer -70 in a byte in 2's complement form as Problems Store -26 and -67 in bytes in 2's complement form Finding the Decimal Integer Values of Positive and Negative Numbers Stored in Bytes in 2's Complement Form Case 1: The MSB of the Byte is 0 If a byte contains the integer in 2's complement form, we know that since the MSB = 0, this is a positive integer. The integer value of the byte then is = +89 in decimal. Case 2: The MSB of the Byte is 1 If a byte contains the integer in 2'complement form, we know that since the MSB = 1, the integer is negative. The quickest way to find the decimal value for the integer is to add the weight of all columns containing a 0 and then add 1 to the result.
24 48 CHAPTER 3 The decimal integer value of is Sign represents the integer -39 (Remember to add +1) Check: Write -39 in 2's complement byte form: Step Procedure Binary Answer in Byte Form 1 2 Write +39 in binary 39 = Take the one's complement (invert the contents of the byte) Add 1 to the LSB + 1 The answer is the two's complement of Therefore we store the integer -39 in a byte in 2's complement form as This is the binary representation in 2's complement form that we were asked to convert to a decimal integer Problem Assignment Determine the integer decimal equivalent of the following byte-sized 2's complement numbers: a) b) c) d) Write the 16 different 2's complement representations of decimal integers that can be stored in a nybble and their corresponding decimal integer values. 2's Complement Representation Decimal Integer
25 Number System and Codes We now have four ways of representing integers in binary: true binary form, signmagnitude notation, 1's complement notation, and 2's complement notation. Complete the following table: A byte contains the following bits a) b) c) d) What decimal integer is stored in the byte for each of the following representations: True binary Signmagnitude 1's Complement. 4. a) Store +6 in 2's complement form using 4 bits. b) Store -7 in 1's complement form using 4 bits. 2's Complement 5. Convert the following numbers stored in 2's complement form in a byte to decimal: a) b) Express the decimal number -39 as an 8-bit number in sign-magnitude, 1's complement, and 2's complement form. 7. How many representations of the integer zero are there in the 2's complement notation using bytes? 8. We have learned to store integers in true binary form, sign-magnitude form, 1's complement form, and 2's complement form. There are additional ways. One other way is called BCD form. Answers Problems Integer to be Stored in a Byte True Binary Representation Sign-Magnitude Representation 1's Complement Representation n/a Problems Store these integers in bytes using the 2's complement notation: a) b) in binary is This integer is too large to be stored in 2's complement form in a byte. Problems Problem Assignment a) b) c) d)
26 50 CHAPTER 3 2's Complement Representation Decimal Integer A byte contains the following bits What decimal integer is stored in the byte for each of the following representations: True binary Signmagnitude 1's Complement 2's Complement a) b) c) d) = a) 0110 b) a) b) sign-magnitude 's complement 's complement Just Two's Complement Arithmetic We have been discussing storing information, particularly integers, in computers mostly in byte form. In this section we look at how addition and subtraction are performed in modern computers. To perform mathematical functions, a computer's central processing unit, or CPU, contains several circuits, and one of these circuits is called the arithmetic-logic unit or ALU.
27 Number System and Codes 51 This unit is the number crunching circuit of every digital system, and, as its name implies, it can be controlled to perform arithmetic operations such as addition and subtraction or logic operations such as AND and OR (which we shall deal with in the next unit). A schematic of a simplified ALU is drawn below. You can see that the arithmetic-logic unit has two parallel inputs, one parallel output, and a set of function select control lines. If we wanted to add two integers, for example, the two integers would be retrieved, probably from memory, and would become inputs to the ALU. The function select part of the ALU would allow the decision to be made to add the numbers. The integers would be added [we shall describe how that addition occurs electronically in the next unit] most likely using 2's complement arithmetic. The sum of the integers would be the output from the ALU. The schematic and the explanation of addition are highly simplified. For example, the Intel Pentium 4 microprocessor has 6 integer execution units, and each of them has a 32-bit ALU. The ALU has to be able to perform the four basic operations, namely addition, subtraction, multiplication and division. One might think that there would be four separate circuits in the ALU, one for each of these operations. That is not the case since all four operations can be performed using a binary-adder circuit. [We shall look at a binary-adder circuit in the next unit when we discuss logic gates.] The fact that all numbers are stored in 2's complement form allows the ALU to use just one circuit for all four basic mathematical operations of addition, subtraction, multiplication and division. In this section we shall show that subtractions can be written as additions. Although we do not discuss multiplication and division in this course, multiplications can be thought of as repeated additions and divisions as repeated subtractions. Having one circuit able to perform all four mathematical operations in the ALU means that the circuit is small and fast to operate. In addition, it consumes very little power and is cheap to manufacture.
28 52 CHAPTER 3 We are now going to look at adding and subtracting in 2's complement notation. Although we shall not look at circuit design until the next unit, we are going to start at this point to look at what would have to be incorporated into a circuit design to deal with the various situations which occur when you add or subtract integers Adding Positive Integers in 2's Complement Form In the examples that follow, variables will be used to represent integers. This is consistent with what actually happens when you process numbers in a computer. When you write a computer program, you may include statements such as cost = 100 sales tax = 7 total cost = cost + sales tax When the program is run, the variable names cost and sales tax and their values would be stored in the computer's memory. The value of the variable total cost would be determined by retrieving the values of cost and sales tax and inputting them to the ALU. After the values of the variables are added in the ALU, the output would be the value of the variable total cost. And, of course, every letter of every variable would be stored in a byte [likely in ASCII form] as a true binary number. The values of the variables would be stored as signed number in 2's complement form. We want to look now at how signed integers such as +100 and +7, stored using 2's complement notation, are added and subtracted. Example If R = +9 and S = +5, find R + S in byte-form using 2's complement. Solution: Each of the integers is written in 2's complement notation [remember that these representations are the same as sign-magnitude notation.] The two integers are then added using regular binary addition. Decimal Addition 2's Complement Addition Overflow in Binary Addition The largest positive integer that can be stored in a byte in 2's complement form is +127, that is Therefore, any time two positive 2's complement integers are added and the sum of the integers is greater than +127 an overflow will occur. This situation is illustrated in Example when +100 and +30 are added.
29 Example If R = +100 and S = +30, find R + S Solution: Decimal Addition Number System and Codes 53 2's Complement Addition The sum of the two integers here is indicated to be Clearly, this sum cannot be a correct representation of the actual sum of +130 because the MSB is 1 which indicates that this binary expression represents a negative number in 2's complement notation. [What negative integer is represented by ? How is this related to the actual sum of +100 and +30?] If we designed a circuit to add two positive numbers, some part of the circuit would have to determine when an overflow occurs. The rule is: 2's complement overflow occurs when the carry into the MSB is not equal to the carry out from the MSB. An overflow detector circuit makes this determination. The schematic below illustrates this concept. The concept of overflow is examined by looking again at Example and Example Example Add +9 and +5 using 2's complement notation. Decimal Addition 2's Complement Addition
30 54 CHAPTER 3 The following graphic illustrates what happens in bits 5, 6 and 7 as they are added. In particular, we are interested in the values of the carry in to byte 7 and the carry out from byte 7. If they are equal, no overflow occurs. Both the carry in and carry out values for bit 7, the MSB, have value 0. No overflow occurs. Example Add +100 and +30. Show that an overflow occurs by checking the carry in to and the carry out from the MSB of the sum. Decimal Addition 2's Complement Addition The carry in to the MSB of 1 is not equal to the carry out of 0 from the MSB. An overflow occurs.