1 Binary Representation and Computer Arithmetic The decimal system of counting and keeping track of items was first created by Hindu mathematicians in India in A.D. 4. Since it involved the use of fingers and thumbs, it was natural that this system would have digits. The system found its way to all the Arab countries by A.D. 8, where it was named the Arabic number system, and from there it was eventually adopted by nearly all the European countries by A.D. 2, 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 number system which means that it uses digits ( 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 5327 would represent a number in base 7 (using the digits to 6) number system. and 2 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 ) number system. The second number is a number in the base 2 number system called the binary number system. The Decimal System This uses ten digits (,, 2, 3, 4, 5, 6, 7, 8, 9) to represent numbers. Consider a decimal number, 847. This means eight hundreds, four tens plus seven. 847 = 8* + 4* + 7 The system has a base or radix of ten. This means that each digit in a decimal number is multiplied by ten raised to a power corresponding to its position. Thus 847 = 8 * * + 7 * Fractional values can be represented in the same way = 4 * * + 2 * + 8 * * -2 The Binary System The binary system uses just two digits, and, as it is easier for a computer to distinguish between two different voltage levels than ten. This means that it has a base of two. Each digit in a binary number is multiplied by two raised to a power corresponding to its position. = * * * 2 + * 2 = * 8 + * 4 + * 2 + = When confusion is possible, we can show the base of a number by using a subscript. For example, 2 = Converting Decimal Numbers to Binary Numbers
2 To convert a decimal number to binary, we can also use repeated division and note the remainder after each division. Example..5 Convert 837 to binary. 837 converted to a binary number: Division Remainder LSD MSD Note that the first remainder gives the LSD and the last remainder the MSD. Then, 837 = 2 Check: 2 = = 837 Octal and Hexadecimal Notation Binary notation is extremely awkward for humans to use, so other notations are common. Decimal notation is of course easy for us to use, but has the disadvantage that conversion between decimal and binary is complicated. One system which is sometimes used is octal. This system uses the first eight digits of the decimal system to represent groups of three bits, and so is base 8. For example, 2 = 53
3 The leading before the number 53 is a common way to indicate an octal number. This has the disadvantage that three does not go into eight, and as we have seen the fundamental unit of information is a chunk of eight bits. Therefore, another commonly used notation is hexadecimal, or simply hex. This breaks the data into nybbles (nibbles) (four bit chunks), so each byte may be represented by two hex digits. Unfortunately, this makes hexadecimal notation base 6 (hence the name), and so we must use the first six letters of the alphabet as well as the ten digits of the decimal system. For example, 2 = xbc The leading x before the number is a common way to indicate a hexadecimal number. Binary Arithmetic The basic rules for binary addition are listed in the table below. Rule Rule 2 Rule 3 Rule 4 Rule The basic rules for binary subtraction are listed in the table below. Rule Rule 2 Rule 3 Rule ALL ABOUT INTEGER REPRESENTATION Computers operate on binary values (as a result of being built from transistors). There are different binary representations for integers possible qualifications:. Positive numbers only 2. Positive and negative numbers 3. Ease of human readability 4. Speed of computer operations There are 4 commonly known ( not common) integer reprentations. All have been used at various times for various reasons.. Unsigned 2. Sign magnitude 3. One's complement 4. Two's complement 5. biased (not commonly known)
4 Virtually all modern computers operate based on 2's complement representation. Why?. Hardware is faster 2. Hardware is simpler (which makes it faster) UNSIGNED The standard binary encoding already given Only positive values Range: to 2**n -, for n bits Example: 4 bits, values to 5 n=4, 2**4 - is 5 Binary dec. hex binary decimal hex a 3 3 b c d e f SIGN MAGNITUDE A human readable way of getting both positive and negative integers. The hardware that does arithmetic on sign magnitude integers is not fast, and it is more complex than the hardware that does arithmetic on 's comp. and 2's comp. integers. Use bit of integer to represent the sign of the integer Let sign bit of be positive, be negative. The rest of the integer is a magnitude, using same encoding as unsigned integers Example: 4 bits is 5 is -5 To get the additive inverse of a number, just flip (not, invert, complement, negate) the sign bit. range: -(2**(n-)) + to 2**(n-) - where n is the number of bits 4 bits, -7 to +7 n=4, - 2**3 + to 2** to 8 -
5 Because of the sign bit, there are 2 representations for. This is a problem for hardware... is, is The computer must do all calculations such that they come out correctly and the same whichever representation is used. ONE's COMPLEMENT Historically important, and we use this representation to get 2's complement integers. Now, nobody builds machines that are based on 's comp. integers. In the past, early computers built by Semour Cray (while at CDC) were based on 's comp. integers. Positive integers use the same representation as unsigned. is is 7, etc. The formal way to determine the 's complement in binary of a negative integer uses the following rule: Rule: The 's complement in binary of a negative integer is obtained by subtracting its magnitude from 2 n - where n is the number of bits used to store the integer in binary. Example Store the integer -36 in a byte in 's complement form. Step : +36 = 2 [convert the magnitude of the integer to binary] Step 2: - A byte contains 8 bits. Therefore, subtract from = 255. In binary 255 is. However, we note the following: Using a byte, the 's complement of +36 is Using a byte, the 's complement of -36 is If these representations are compared bit by bit, one can see that corresponding bits are inverted. Negation (finding an additive inverse) is done by taking a bitwise complement of the positive representation. COMPLEMENT. INVERT. NOT. FLIP. NEGATE. A logical operation done on a single bit The complement of is. The complement of is. - --> take +, Complement each bit
6 That is -. Don t add or take away any bits. EXAMPLES: this must be a negative number. to find out which, find the additive inverse! is +3 by sight, so must be -3 Things to notice:. any negative number will have a in the MSB. 2. There are 2 representations for, and. Converting from 's Complement Notation to Decimal Notation Case : The MSB of the Byte is If a byte contains, for example, which represents an integer stored in 's complement form, we know that since the MSB =, this is a positive integer. The decimal integer value of the byte then is : This indicates the integer is positive Therefore which represent a binary number in 's complement form is the decimal integer = +89 Case 2: The MSB of the Byte is Add the Weight of all Bits Containing a Convert to decimal. The MSB has value. Therefore, the integer is negative. Bits 5, 4, 3 and have value. Therefore, the integer value is -( ) = -57. These steps are summarized in the following table: Binary Number in 's Complement Notation Weight of Bits Containing 's This is the sign bit indicating the integer is negative Therefore which represents a binary number stored in 's complement notation has decimal value -( ) = -57 TWO's COMPLEMENT
7 Rule: To determine the 2's complement of a negative integer, determine the 's complement and add. A variation on 's complement that does NOT have 2 representations for. This makes the hardware that does arithmetic faster than for the other representations. A 3 bit example: Bit pattern: 's comp: 's comp.: The negative values are all "slid" down by one, eliminating the extra zero representation. Example Store -27 in a byte using 2's complement notation. Steps and 2 determine the 's complement: Step : +27 in binary is. Step 2: Invert bits to yield. Then the 's complement of -27 is. Step 3 is the additional step needed to find the 2's complement. Step 3: Add to the 's complement: + The 2's complement of -27 is. Finding the Decimal Integer Values of Positive and Negative Numbers Stored in Bytes in 2's Complement Form Case : The MSB of the Byte is If a byte contains the integer in 2's complement form, we know that since the MSB =, this is a positive integer. The integer value of the byte then is = +89 in decimal. Case 2: The MSB of the Byte is If a byte contains the integer in 2'complement form, we know that since the MSB =, 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 and then add to the result. The decimal integer value of is Sign
8 represents the integer -39 (Remember to add +) Check: Write -39 in 2's complement byte form: Step Procedure 2 Write +39 in binary 39 = Take the one's complement (invert the contents of the byte) Binary Answer in Byte Form 3 Add to the LSB + The answer is the two's complement of -39. 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. BIASED REPRESENTATION An integer representation that skews the bit patterns so as to look just like unsigned but actually represents negative numbers. Examples: given 4 bits, we BIAS values by 2**3 (8) TRUE VALUE to be represented 3 Add in the bias Unsigned value So the bit pattern of 3 in 4-bit biased-8 representation will be Going the other way, suppose we were given a biased-8 representation as Unsigned represent 6 Subtract out the bias TRUE VALUE represented -2 This representation allows operations on the biased numbers to be the same as for unsigned integers, but actually represents both positive and negative values. Choosing a bias: The bias chosen is most often based on the number of bits available for representing an integer. To get an approx. equal distribution of true values above and below, the bias should be 2 ** (n-) or (2**(n-)) - Two's Complement Arithmetic 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.
9 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 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. 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. 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. Multiplications can be thought of as repeated additions and divisions as repeated subtractions. Adding Positive Integers in 2's Complement Form
10 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 = 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 + and +7, stored using 2's complement notation, are added and subtracted. Example.6. If R = +9 and S = +5, find R + S in byte-form using 2's complement notation. 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 +27, that is. Therefore, any time two positive 2's complement integers are added and the sum of the integers is greater than +27 an overflow will occur. This situation is illustrated in Example.6.2 when + and +3 are added. Example.6.2 If R = + and S = +3, find R + S Solution: Decimal Addition 2's Complement Addition
11 +3 The sum of the two integers here is indicated to be. Clearly, this sum cannot be a correct representation of the actual sum of +3 because the MSB is 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 + and +3?] 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.6. and Example.6.2. Example.6. Add +9 and +5 using 2's complement notation. Decimal Addition 2's Complement Addition 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.
12 Both the carry in and carry out values for bit 7, the MSB, have value. No overflow occurs. Example.6.2 Add + and +3. 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
13 The carry in to the MSB of is not equal to the carry out of from the MSB. An overflow occurs. Subtraction with One's Complement Let's consider how we would solve our problem of subtracting from 7 using 's complement.. First, we need to convert 2 to its negative equivalent in 's complement. 2. To do this we change all the 's to 's and 's to 's. Notice that the most-significant digit is now since the number is negative. 3. Next, we add the negative value we computed to 2. This gives us a result of Notice that our addition caused an overflow bit. Whenever we have an overflow bit in 's complement, we add this bit to our sum to get the correct answer. If there is no overflow bit, then we leave the sum as it is. 5. This gives us a final answer of 2 (or 6 ). (7) - - () -> (7) + +(-) (?) + (6) (7) - - () (6) Now let's look at an example where our problem does not generate an overflow bit. We will subtract 7 from using 's complement.. First, we state our problem in binary. () - - (7)
14 2. Next, we convert 2 to its negative equivalent and add this to This time our results does not cause an overflow, so we do not need to adjust the sum. Notice that our final answer is a negative number since it begins with a. Remember that our answer is in 's complement notation so the correct decimal value for our answer is -6 and not 9. () + +(-7) (?) () + +(-7) (-6) REPRESENTATION OF FLOATING POINT NUMBERS Computers represent real values in a form similar to that of scientific notation. There are standards which define what the representation means so that across computers there will be consistency. Note that this is not the only way to represent floating point numbers, it is just the IEEE standard way of doing it. Here's what we do: the representation S E F S is one bit representing the sign of the number E is an 8 bit biased integer representing the exponent F is an unsigned integer The value represented is: (-) S x F x 2 E Where e = E - bias f = F/2 n + For single precision numbers (the emphasis in this class) n = 23 Bias = 27 Now, what does all this mean? (Everything here is for IEEE single precision floating point representation.) --> S, E, F all represent fields within a representation. Each is just a bunch of bits. --> S is just a sign bit. for positive, for negative. --> E is an exponent field. The E field is a biased-27 representation. So, the true exponent represented is (E - bias). The radix for the number is ALWAYS 2.
15 Note: Computers that did not use this representation, like those built before the standard, did not always use a radix of 2. Example: some IBM machines had radix of 6. --> F is the mantissa. It is in a somewhat modified form. There are 23 bits available for the mantissa. It turns out that if fl. pt. numbers are always stored in a normalized form, then the leading bit (the one on the left, or MSB) is always a. So, why store it at all? It gets put back into the number (giving 24 bits of precision for the mantissa) for any calculation, but we only have to store 23 bits. This MSB is called the HIDDEN BIT. An example: put the decimal number 64.2 into the IEEE single precision representation. First step: Get a binary representation for 64.2 To do this, get binary reps. for the stuff to the left and right of the decimal point separately. 64 is.2 can be gotten using the algorithm:.2 x 2 =.4 (msb).4 x 2 =.8.8 x 2 =.6.6 x 2 =.2.2 x 2 =.4 now this whole pattern () repeats..4 x 2 =.8.8 x 2 =.6.6 x 2 =.2 So a binary representation for.2 is.... Putting the halves back together again: 64.2 is.... Second step: Normalize the binary representation. (make it look like scientific notation) x 2 Third step: Represent exponent in the standard form(biased) 6 is the true exponent. For the standard form, it needs to be in biased-27 form (value of biased exponent) 33 in 8 bit, unsigned representation is
16 This is bit pattern used for E in the standard form. Fourth step: The mantissa stored (F) is the stuff to the right of the radix point in the normal form. We need 23 bits of it. Put it all together (and include the correct sign bit): S E F The values are often given in hex, so here it is x Or h Some extra details: --> Since floating point numbers are always stored in normal form, how do we represent? (What does the fl. pt. number x3f8 represent?) (What does the fl. pt. number x8 represent?) We take the bit patterns x and x8 to represent the value. (What fl. pt. numbers cannot be represented because of this?) --> Other special values: +infinity... (x7f8 ) -infinity... (xff8 ) NaN (Not a Number)??????... (S is either or, E=xff, and F is anything but all zeros) Example Notation Value Ordinary Decimal Scientific Decimal.7825 E 2 Scientific Binary. E 2 Scientific Binary (Biased Exponent). E 2 Single-Real Format Sign Biased Exponent Normalized Significand (. implied => J-Bit)
17 When performing normalized floating-point computations, an FPU normally operates on normalized numbers and produces normalized numbers as results. Denormalized numbers represent an underflow condition. A denormalized number is computed through a technique called gradual underflow. Table 2 gives an example of gradual underflow in the denormalization process. Here the single-real format is being used, so the minimum exponent (unbiased) is -26. The true result in this example requires an exponent of -29 in order to have a normalized number. Since -29 is beyond the allowable exponent range, the result is denormalized by inserting leading zeros until the minimum exponent of -26 is reached. Operation Sign Exponent* Significand True Result Denormalize Denormalize Denormalize Denormal Result NOTE: * Expressed as an unbiased, decimal number. Table 2: Denormalization Process In the extreme case, all the significant bits are shifted out to the right by leading zeros, creating a zero result. The FPU deals with denormal values in the following ways: It avoids creating denormals by normalizing numbers whenever possible. It provides the floating-point underflow exception to permit programmers to detect cases when denormals are created. It provides the floating-point denormal-operand exception to permit procedures or programs to detect when denormals are being used as source operands for computations. BINARY CODED DECIMAL In today s technology, you hear a great deal about microprocessors. A microprocessor is an integrated circuit designed for two purposes: data processing and control. Computers and microprocessors both operate on a series of electrical pulses called words. A word can be represented by a binary number such as 2. Binary-coded decimal, or BCD, is a method of using binary digits to represent the decimal digits through 9. A decimal digit is represented by four binary digits, as shown below:
18 Example Represent 49 in BCD 4 -> 9 -> Therefore, 49 in BCD is BCD REFERENCES