Introduction to Computer Architecture Data...

40
Introduction to Computer Architecture Data Representation 1 4. Data Representation Character Representation Character representation resolves the question of how to carry non-numeric data around within the hardware of a computer system. All information within the circuitry of the computer is represented by one of two electrical or electronic states, which we conveniently refer to as ‘1's and ‘0's. These are the two digits of the binary number system which we discuss in the next section, and are referred to as ‘bits’ (short for binary digits). This ‘numeric’ interpretation of the state of the machine must somehow be interpreted as character data for the purpose of storing, retrieving and manipulating real-world information aside from numerical computation. The common way to accomplish character representation in a number-based system is to ‘encode’ the characters of interest as specific combinations of bits. There are a number of common ways in which this is done in practice, three of which we mention here. ASCII - This stands for the American Standard Code for Information Interchange. This was an originally a seven-bit code (i. e. each character was represented by a different combination of 7 ‘1's and ‘0's), seven bits being chosen because this allowed 2 7 = 128 different characters (and control codes) to be represented. For instance, 100 0001 is used to represent the upper case letter ‘A’ 100 0010 is used to represent the upper case letter ‘B’ 110 0001 is used to represent the lower case letter ‘a’ 010 0000 is used to represent a space (‘ ’) and so on. When the IBM PC was introduced in 1981 it expanded the code by adding an eighth bit which now allowed 256 different characters. This became known as the Extended ASCII code, but has now become the de facto standard for the ASCII code. All of the existing code points now have an additional ‘0' in the high-order bit position; In this extended ASCII code. 0100 0001 is used to represent the upper case letter ‘A’ which simply adds a 0 to the front of the originally 7-bit code. the 128 new codes with a ‘1' in that position are not standardized, but are architecture-dependant. They can be assigned new character meanings, such as graphical and iconic symbols (e.g. the copyright symbol, © ), or an alternate character set. In some cases, only the basic 128

Transcript of Introduction to Computer Architecture Data...

Page 1: Introduction to Computer Architecture Data Representationfaculty.msmc.edu/christen/comp_ops-mgmt/data_representation.pdf · Introduction to Computer Architecture Data Representation

Introduction to Computer Architecture Data Representation

1

4. Data Representation

Character Representation

Character representation resolves the question of how to carry non-numeric dataaround within the hardware of a computer system. All information within the circuitry ofthe computer is represented by one of two electrical or electronic states, which weconveniently refer to as ‘1's and ‘0's. These are the two digits of the binary numbersystem which we discuss in the next section, and are referred to as ‘bits’ (short forbinary digits). This ‘numeric’ interpretation of the state of the machine must somehowbe interpreted as character data for the purpose of storing, retrieving and manipulatingreal-world information aside from numerical computation.

The common way to accomplish character representation in a number-based system isto ‘encode’ the characters of interest as specific combinations of bits. There are anumber of common ways in which this is done in practice, three of which we mentionhere.

ASCII - This stands for the American Standard Code for Information Interchange. Thiswas an originally a seven-bit code (i. e. each character was represented by a differentcombination of 7 ‘1's and ‘0's), seven bits being chosen because this allowed 27 = 128different characters (and control codes) to be represented. For instance,

100 0001 is used to represent the upper case letter ‘A’100 0010 is used to represent the upper case letter ‘B’110 0001 is used to represent the lower case letter ‘a’010 0000 is used to represent a space (‘ ’)

and so on.

When the IBM PC was introduced in 1981 it expanded the code by adding an eighth bitwhich now allowed 256 different characters. This became known as the ExtendedASCII code, but has now become the de facto standard for the ASCII code. All of theexisting code points now have an additional ‘0' in the high-order bit position; In thisextended ASCII code.

0100 0001 is used to represent the upper case letter ‘A’

which simply adds a 0 to the front of the originally 7-bit code. the 128 new codes with a‘1' in that position are not standardized, but are architecture-dependant. They can beassigned new character meanings, such as graphical and iconic symbols (e.g. thecopyright symbol, © ), or an alternate character set. In some cases, only the basic 128

Page 2: Introduction to Computer Architecture Data Representationfaculty.msmc.edu/christen/comp_ops-mgmt/data_representation.pdf · Introduction to Computer Architecture Data Representation

Introduction to Computer Architecture Data Representation

Data RepresentationNTC 10/26/04 2

codes are used and the extra bit is used as a parity bit for error checking (see thechapter on Error Correcton.)

EBCDIC - standing for the Extended Binary Coded Decimal Interchange Code, this issimilar in usage to the ASCII code. Unlike ASCII, it was initially conceived as an eightbit code and is currently the standard code for character representation in IBMmainframe computers, whereas ASCII is used primarily in Personal Computers (onceknown as microcomputers.). The EBCDIC code is based on being able to representthe decimal digits in packed BCD format (see the next section), which optimizes certainoperations for business applications.

Unicode - Since not all languages are based on the Latin alphabets, a code is neededto represent things such as the Kanji (japanese) characters, Persian and Hebrewalphabets, etc. The Unicode provides this by using a 16-bit code, rather than an 8-bitcode allowing for 216 = 64K characters to be represented.

Number Systems

Introduction to counting and positional notation.

In the decimal number system, a typical number, N, looks like

. . . d3d2d1d0.d-1d-2d-3 . . . [N1]

where the ellipsis at each end indicates that there is no limit on the number of digits anumber might have. Each di is a decimal digit which can only take on the values 0through 9. Consider just the integer portion of a decimal number having 4 digits: d3d2d1d0. In a positional number system each digit has a weight, depending on whichcolumn (or position) it occupies in the number. Thus, d0 is in the ones column, d1 is inthe tens column, d2 is in the hundreds column, etc. Now, note that 1 = 100, 10 = 101,100 = 102, 1000 = 103, and so on.

We could say, therefore, that d0 has a weight of 100, d1 has a weight of 101, d2 has aweight of 102, etc. When we consider what the actual value of d3d2d1d0 is we mentallyperform the following calculation:

d3 x 103 + d2 x 102 + d1 x 101 +d0 x 100 [N2]

So, for example, the number 538 is ‘five hundred and thirty eight’.

Algebraically, [N2] can be rewritten as:

Page 3: Introduction to Computer Architecture Data Representationfaculty.msmc.edu/christen/comp_ops-mgmt/data_representation.pdf · Introduction to Computer Architecture Data Representation

Introduction to Computer Architecture Data Representation

Data RepresentationNTC 10/26/04 3

3

�di10i [N3] i=0

Similarly, the fractional part of the decimal number given in [N1], .d-1d-2d-3, is interpretedas

d-1 x 10-1 + d-2 x 10-2 +d-3 x 10-3

since d-1 is in the tenths column, d-2 is in the hundredths column, etc. Similarly to [N3],the fractional part of the number can be expressed as

-3

�di10i [N4] i=-1

We say that the base of the decimal number system is ten, represented by the symbol10. We can also use the term radix as a substitute for ‘base’. However, there isnothing magical about the number ten, and any other number can serve as the base, orradix, of a number system. The first thing to remember is that whatever the base (callit r for generality), there are only r digits in the number system: 0 through r-1.

Consider, for example, a number system based on base 8. The only digits available insuch a system are 0, 1, 2, 3, 4, 5, 6, and 7. When counting, when we run out of digits,we note this by putting a 1 in the next column and starting over at 0 in the first column. Just as in decimal. The numbers zero through sixteen in base 8 notation are

0 zero 10 eight 1 one 11 nine 2 two 12 ten 3 three 13 eleven 4 four 14 twelve 5 five 15 thirteen 6 six 16 fourteen 7 seven 17 fifteen [N5] 20 sixteen

In the base 8 system, just as in the decimal system, each column has a weight, basedon which column it is in. The rightmost column in a base 8 integer is the ones column,the next column is the eights column, the third column is the sixty-fours column. In

Page 4: Introduction to Computer Architecture Data Representationfaculty.msmc.edu/christen/comp_ops-mgmt/data_representation.pdf · Introduction to Computer Architecture Data Representation

Introduction to Computer Architecture Data Representation

Data RepresentationNTC 10/26/04 4

general, the weight of each column is the number eight raised to the power of thecolumn, starting with zero. This is identical to the decimal system. So, a three digitoctal number, d2d1d0, is interpreted, similarly to [N2] as

d2 x 82 + d1 x 81 + d0 x 80 [N6]

which can be expressed similarly to [N3] as

2

�di8i [N7]

i=0

Fractional parts of numbers in the base eight system follow the same scheme, so thatthe first fractional digit is the eighths column (8-1), followed by the sixty-fourths (8-2)column, and so on.

These remarks are valid for any integer you care to choose as the base of a positionalnumber system, so we can make the following generalizations:

a. If r is the base (radix) of a number system, the only digits, di, available inthat number system are 0, 1, . . . r-1.

b. The value of any number, N, in such a number system is given by

+k

�diri [N8]

i= -j

where d-j is the least significant digit and dk is the most significant digit.

It is often difficult, especially when talking about multiple number systems, to knowwhich system a number is being represented in, For instance, how do I know that 1100is a binary number (twelve) and not a trinary (or ternary) number (thirty-six), an octalnumber (Five hundred seventy six), or decimal (one thousand one hundred)? We willuse a decimal subscript to indicate the base (radix): 11002 = 1210 = 148 = C16.

Notes:

Regardless of the number system, the rules of arithmetic do not change. As longas you remember the number of digits you have available in the number system ofchoice you should be able to do simple addition, subtraction and multiplication in anynumber system (division is harder, not because it is different, but because we are notused to thinking in the alternative number system.) Here are three examples in radix 6.

Page 5: Introduction to Computer Architecture Data Representationfaculty.msmc.edu/christen/comp_ops-mgmt/data_representation.pdf · Introduction to Computer Architecture Data Representation

Introduction to Computer Architecture Data Representation

Data RepresentationNTC 10/26/04 5

1536 3236 456 + 2146 - 1156 x 326 ------------ ------------- ----------- 4116 1046 134

223 ----------- 24046

Regardless of the number system, the radix itself is always represented by 10r inthat radix (that is, r10 = 10r). We need to break ourselves of the habit of thinking 10 =‘ten’ and instead think of it as ‘one-zero’, with the appropriate meaning in the numbersystem at hand. Some examples:

810 = 108 510 = 105 1610 = 1016 210 = 102

Similarly, r2 = 100r, r3 = 1000r, etc. regardless of the value of r.

As in the decimal system, rn is equal to 1 followed by n zeros.

2410 = 100002 85

10 = 1000008 16210 = 10016

Multiplying a number by r in any number system adds a zero to the end of thenumber, just as in base 10. More accurately, multiplying by r moves the radix point(decimal point in base 10, binary point in base 2, etc.) one place to the right. (And ofcourse dividing by r moves the radix point one place to the left.)

1234 x 104 = 12304 10112 x 102 = 101102

In general,

dn-1 ..d1d0 x r = dn-1...d1d00 (or dn...d1d0 where d0 = 0)

Binary and Hexadecimal Number systems

The binary number system is one with a radix of 2. It has only two digits, 0 and 1. Binary numbers are important in computer science because of the nature of digitalcomputers. Digital computers are built out of devices which generally have only twostates, like a switch has only the states on and off. Electrical devices may have apositive or negative voltage, current may be flowing or not flowing, magnetic fields mayflow in one of two directions and so on. Transistors are used a switches throughout acomputer system and they either conduct current or they don’t. These two states arethe on and off of a switch. The two states of electronic devices are generally given

Page 6: Introduction to Computer Architecture Data Representationfaculty.msmc.edu/christen/comp_ops-mgmt/data_representation.pdf · Introduction to Computer Architecture Data Representation

Introduction to Computer Architecture Data Representation

Data RepresentationNTC 10/26/04 6

conceptual values depending on what they are intended to represent, such as yes andno, or true and false. For conciseness as well as because there is useful mathematicalmeaning, we generally assign the symbols 0 and 1 to these two states. Here are thefirst sixteen binary numbers:

0 zero 1000 eight1 one 1001 nine10 two 1010 ten11 three 1011 eleven100 Four 1100 twelve101 five 1101 thirteen110 six 1110 fourteen111 seven 1111 fifteen

Hexadecimal numbers are numbers in a number system with radix of 16. Note that,since there is nothing magical about the number ten, there is no requirement thatnumber system bases be less than ten. The problem with larger number bases, ofcourse, is that we don’t have symbols for digits greater than nine.

Since we need sixteen digits for the hexadecimal number system, we use the letters A,B, C, D, E, and F to stand for the digits ten, eleven, twelve, thirteen, fourteen, andfifteen, respectively. Here are the first sixteen numbers expressed in the hexadecimalsystem:

0 zero 8 eight 1 one 9 nine 2 two A ten 3 three B eleven 4 four C twelve 5 five D thirteen 6 six E fourteen 7 seven F fifteen [N9]

10 sixteen

Page 7: Introduction to Computer Architecture Data Representationfaculty.msmc.edu/christen/comp_ops-mgmt/data_representation.pdf · Introduction to Computer Architecture Data Representation

Introduction to Computer Architecture Data Representation

Data RepresentationNTC 10/26/04 7

NUMBER DECIMAL BINARY HEXADECIMAL OCTAL

Zero 0 0000 0 0

One 1 0001 1 1

Twp 2 0010 2 2

Three 3 0011 3 3

Four 4 0100 4 4

Five 5 0101 5 5

Six 6 0110 6 6

Seven 7 0111 7 7

Eight 8 1000 8 10

Nine 9 1001 9 11

Ten 10 1010 A 12

Eleven 11 1011 B 13

Twelve 12 1100 C 14

Thirteen 13 1101 D 15

Fourteen 14 1110 E 16

Fifteen 15 1111 F 17

Table TN1.

The table TN1 shows the numbers zero thru fifteen in all the number systems mostcommonly used in computer science.

Radix Conversion

It is often necessary (in Computer Science, at least) to convert numbers from onenumber system to another. For instance, the fact that the value of the binary number111001012 is 22910 is not immediately obvious. We present here a general procedurefor converting a number in one radix, ro, (o for old) to a number in radix rn (n for new).

Page 8: Introduction to Computer Architecture Data Representationfaculty.msmc.edu/christen/comp_ops-mgmt/data_representation.pdf · Introduction to Computer Architecture Data Representation

Introduction to Computer Architecture Data Representation

Data RepresentationNTC 10/26/04 8

Converting Integers

Assume we have a number in radix ro and we want to convert to a new radix rn. Clearlyeach digit in the number in the new base must be less than rn (0 thru rn-1), and this willbe true if we generate the digits for the new number by dividing the old number by thenew radix (written in the old base) and retaining the remainder. Recall that this is theMOD operation. We also determine Q, using the DIV operator.

We then iteratively use the DIV and MOD operators on the new Q generated each time. Each remainder produced by MOD is a subsequent digit of the number in the new radix;each Q produced by DIV is used for the next iteration’s MOD. It is important to notethat the digits are produced least significant first.

Here is the algorithm in more compact form:

Q0 = N div rn d0 = N mod rnQ1 = Q0 div rn d1 = Q0 mod rn

Qi = Qi-1 div rn di = Qi-1 mod rn

Continue until Qi = 0.

In this text we are only concerned with conversions between base 2 and base 10.

Examples:

1. Convert 4310 to Base 2

N =4310 rn = 210Q0 = 43 div 2 = 21 d0 = 1Q1 = 21 div 2 = 10 d1 = 1Q2 = 10 div 2 = 5 d2 = 0Q3 = 5 div 2 = 2 d3 = 1Q4 = 2 div 2 = 1 d4 = 0Q5 = 1 div 2 = 0 d5 = 1

Since Q5 = 0 we are done. The result is

4310 = 1010112

Note that digits are generated starting at the radix (binary) point.

Page 9: Introduction to Computer Architecture Data Representationfaculty.msmc.edu/christen/comp_ops-mgmt/data_representation.pdf · Introduction to Computer Architecture Data Representation

Introduction to Computer Architecture Data Representation

Data RepresentationNTC 10/26/04 9

2. Convert 110012 to Base 10.

N = 110012 rn = 10

First we must convert the new base to the base 2 number system (using thetechniques exemplified in example 1).

rn = 1010 = 10102Then

Q0 = 110012 div 10102 = 102 d0 = 1012Q1 = 102 div 10102 = 0 d1 = 102

Since Q1 is 0, the algorithm terminates. But since the di are in base 2 we need toconvert them to decimal digits. We will see a shortcut for doing this shortly; for nowaccept that

d0 = 1012 = 510 andd1 = 102 = 210

so that

110012 = 2510

Whenever converting to base 10 from base 2 there is a vastly simpler method. Remember that converting a number in an arbitrary radix number into the base 10number system is easily done by evaluating the expression �dir

i ([N8]) in general and�di2

i in particular for converting from base 2. Let’s redo the last example:

Examples:

2a. Convert 110012 to base 10

110012 = 1 x 24 + 1 x 23 + 0 x 22 + 0 x 21 + 1 x 20

= 1 x 16 + 1 x 8 + 0 x 4 + 0 x 2 + 1 x 1= 16 + 8 + 1= 2510

This is the method we used to convert the digits in example 2 above to decimal.

Another special case exists when both old and new radix are powers of the same base

Page 10: Introduction to Computer Architecture Data Representationfaculty.msmc.edu/christen/comp_ops-mgmt/data_representation.pdf · Introduction to Computer Architecture Data Representation

Introduction to Computer Architecture Data Representation

Data RepresentationNTC 10/26/04 10

number. For example base 16 and base 8 are both powers of base 2. Do it in twosteps: 1) convert the number to the common base, then 2) convert to the new radix. Why is this simple? Because converting from, say, 16 to base 2 requires nocomputation, nor vice versa.

Example:

8. Convert base 16 to base 8: D5A16 = 65328

1. Convert to base 2: 1101 0101 10102. Regroup bits (starting from right to left) into groups containing 3 bits:110 101 011 0103. Convert each group of bits to base 8: 65328

It is common to use hexadecimal numbers as shorthand for binary numbers incomputer science. Memory addresses, for instance, are 32 bits in size in modern PCs(and 64 bits in most workstations). It is much more convenient to work with 8 hex digitsthan it is to work with 32 binary digits.

Example:

9. represent the 32 bit binary address 10010101010111111000110011010001 inhexadecimal.

Four bit groups = 1001 0101 0101 1111 1000 1100 1101 00012Using [TN1] = 955F8CB116

Practice Problems - Integer Number System Conversions

1. Convert the decimal integer 42710 into Binary (base 2)

2. Convert 1001111012 to a decimal integer.

3. Convert the following decimal integers to binary:

a. 2 c. 27 e. 128 g. 31b. 245 d. 127 f. 17 h. 257

Page 11: Introduction to Computer Architecture Data Representationfaculty.msmc.edu/christen/comp_ops-mgmt/data_representation.pdf · Introduction to Computer Architecture Data Representation

Introduction to Computer Architecture Data Representation

Data RepresentationNTC 10/26/04 11

Converting Fractions

Converting Fractions uses iterative multiplication instead of modulo division. Multiplythe fraction, N, by the new radix, rn (in the old radix representation), to get a product P;if the result is greater than one, the integer part is the next fractional digit. If it is lessthen one, zero is the next fractional digit. The fractional part, after multiplication, isused for the next multiply. The digits produced are the most significant to the leastsignificant (i. e. starting at the radix point).

Example:

10. Convert decimal .875 to base 2:

P-1 =.875 x 2 = 1.750 d-1 = 1P-2 = .750 x 2 = 1.5 d-2 = 1P-3 = .5 x 2 = 1.0 d-3 = 1

At this point the next fraction to be multiplied is 0, so there is no point in continuing: weare done.

In practice, the conversion may not terminate! There are three situations which mayoccur when converting fractions:

2. The remaining fraction is zero. You are done.

Convert .7510 to binary.

.75 x 2 = 1.50 d-1 = 1

.50 x 2 = 1.00 d-2 = 1

Since the fraction is now zero, we can stop. .7510 = .112

3. The remaining fraction repeats a previously obtained fraction. Youcan stop, realizing that the answer is a repeating fraction in the new base.

Convert .3 to binary.

.3 x 2 = 0.6 d-1 = 0

.6 x 2 = 1.2 d-2 = 1

.2 x 2 = 0.4 d-3 = 0

.4 x 2 = 0.8 d-4 = 0

.8 x 2 = 1.6 d-5 = 1

Page 12: Introduction to Computer Architecture Data Representationfaculty.msmc.edu/christen/comp_ops-mgmt/data_representation.pdf · Introduction to Computer Architecture Data Representation

Introduction to Computer Architecture Data Representation

Data RepresentationNTC 10/26/04 12

.6 x 2 = 1.2 d-6 = 1

Since the fraction (.6) has repeated we can stop. After the initial 0, theanswer is a repeating fraction with 1001 repeating forever:0100110011001. . .

4. Neither of the above situations occurs, and you get tired. You mayhave a non-repeating, non-ending fraction (like pi). It is also possible thatyou have a repeating fraction with a very large number of positionsrepeating.

Convert decimal .834 to base 3:

P-1 =.834 x 2 = 1.668 d-1 = 1P-2 = .668 x 2 = 1.336 d-2 = 1P-3 = .336 x 3 = 0.672 d-3 = 0P-4 = .672 x 3 = 1.344 d-4 = 1P-5 = .344 x 3 = 0.388 d-5 = 0P-6 = .388 x 3 = 0.776 d-6 = 0P-7 = .776 x 3 = 1.552 d-7 = 1

and so on. The fraction .83410 = .1101001...2 with no end in sight.

Note that, as a practical matter, the number of decimal digits you generateis determined by the requirements of the problem you are solving. Forany given problem, it only makes sense to retain the number ofsignificant digits required by the application.

Again, when the new radix is 10, it is easiest to use [N8]: �di2i .

Example:

11. .112 = 1 x 2-1 + 1 x 2-2 = 1x ½ + 1 x 1/4 = 3/4 = .7510

As with integer numbers, when both radixes are powers of a common base it may beeasier to convert first to the common base and then to the new base. For examplebase 2 and base 16 because 16 = 24. In this case, convert groups of four binary digitsto hex digits, or vice versa. Converting between base 8 and 16, just convert first tobinary, then to the desired base.

Example: Convert .DA16 to ?8

Page 13: Introduction to Computer Architecture Data Representationfaculty.msmc.edu/christen/comp_ops-mgmt/data_representation.pdf · Introduction to Computer Architecture Data Representation

Introduction to Computer Architecture Data Representation

Data RepresentationNTC 10/26/04 13

.DA16 = .110110102 = .110 110 10 = .6628

Note that grouping is always done starting at the radix point.

Practice Problems - Fraction Number System Conversion

1. Convert the following fractions to binary:

a. .875 b. .2 c. .71

2. Convert the following fractions from the binary to decimal:

a. .011012 b. .000012

Negative Binary Numbers

In what follows, keep in mind that, since we are interested in using these numberswithin the hardware of a computer, there is a finite amount (usually small) of space(storage) allocated for each number. In the computer’s memory this may be referred toas a ‘word’. In the CPU such data is held in storage devices called ‘registers’. Eachregister has a finite size, measured in ’bits’, where each bit is a binary digit (in fact, theword bit is a contraction of the words ‘binary digit’. We assume that every binarynumber uses all the bits available in the specified register. A group of adjacent 8 bits ina register is called a ‘byte’. A 32-bit register, therefore, is also a 4-byte register.

There are six common ways of representing a number in binary:

UnsignedSign and Magnitude2's complementBiasedBinary Coded DecimalFloating Point

The second, third and fourth are binary representations which allow for therepresentation of negative as well as positive numbers.

5. Unsigned binary numbers. All the bits in the register are weighted per there bitpositions, and are numbered 0 through n-1 from right to left . If the register is n

Page 14: Introduction to Computer Architecture Data Representationfaculty.msmc.edu/christen/comp_ops-mgmt/data_representation.pdf · Introduction to Computer Architecture Data Representation

Introduction to Computer Architecture Data Representation

Data RepresentationNTC 10/26/04 14

bits wide, it can hold 2n different values, in the range (assuming integers only) of0 through +2n - 1. For example, a 16-bit register holds 216 = 65536 values, 0 thru65545. Note that all arithmetic results which end up in an n-bit register cannever have a value greater than 2n - 1, and is thus effectively arithmetic modulo2n. Keep this in mind for future discussion.

6. Sign and Magnitude. In this convention, only bits 0 through n-2 are used to holdthe number, bit n-1 (the most significant bit) is used to specify the sign of thenumber. A zero in position n-1 means that the number is positive, a one inposition n-1 means that the number is negative. In an eight bit register, forexample, bit 7 represents the sign. The number 001111002 is the number + 6010(the same as if the number were unsigned), and 101111002 is - 6010 (this wouldbe 18810 if it were an unsigned number). The range of values able to berepresented in this form is -(2n-1-1) through +2n-1-1. The values in a 16 bit registerare -32767 thru +32767. One problem with this convention is that 010 has tworepresentations: 00000002 and 100000002.

7. 2's complement. Both 2's complement and 1's complement may be used toincorporate both positive and negative number representations in a computer; wewill focus on 2's complement for reasons that will become clear later. As in thepreviously mentioned conventions, positive numbers (and zero) are representednormally, just as if they were unsigned binary numbers. However, the negative ofa number in 2's complement form is simply the value you get when you subtractthe positive representation of the number from 2n. That is, if x is binary n-bitnumber, then

-x = 2n - x [N10]

There is a simple way to perform this operation without actually having to dosubtraction, consisting of a two step process as follows:

i. Complement the value. That is, change every 1 to a 0 and every 0to a 1.

ii. Add 1 to the result of the first step.

For example, in an eight bit register, the number +5610 is given by 001110002. Complementing this gives 110001112. Adding 1 gives 110010002 which is usedto represent -5610.

Recall that when converting a binary number to a decimal number, we simply addup all the weighted values of the positions in the binary where there is a 1. Toconvert a 2's complement number, the same procedure is applied, but the weight

Page 15: Introduction to Computer Architecture Data Representationfaculty.msmc.edu/christen/comp_ops-mgmt/data_representation.pdf · Introduction to Computer Architecture Data Representation

Introduction to Computer Architecture Data Representation

Data RepresentationNTC 10/26/04 15

of the (n-1)st bit (the most significant bit) has a weight of -2n-1 instead of +2n-1!. Thus the decimal value of the four-bit 2's complement number 1110 is -23 + 22 +21 = -8 + 4 + 2 = -2.

The range of values able to be represented in 2's complement is -2n-1 through +2n-

1-1. This is one more negative value than is represented by the sign & magnituderepresentation, corresponding to the fact that there is only one representation forzero.

Note 1: When this convention is being used, all numbers are considered ‘2'scomplement’ numbers, regardless of whether they are positive or negative. Don’tfall into the trap of thinking that only the negative numbers are in 2's complement.

Note 2: Negative numbers can be converted to positive numbers using the exactsame procedure: complement and add 1 (i.e. you don’t have to do the operationsin reverse by subtracting 1 and then complementing).

Note 3: Negative numbers will always have a leading 1, positive numbers willalways have a leading 0. If you are asked to convert a positive decimal numberto a 2's complement number, don’t forget to add the leading 0!

Note 4: When a positive n-bit number and its negative representation (in 2'scomplement form) are added together, the result is 2n (see N10]). Since themaximum value an n-bit register can hold is 2n-1, the result is all zeros in theregister and an ‘overflow’ (This is an addition modulo 2n). We ignore the overflowand get the correct result: zero. As we will see this is important for implementingbinary arithmetic.

Note 5: A 2's complement integer n bits in length can be represented similarly to[N8] except that the weight of the most significant bit is negative.

N = (- dn-1 x -2n-1) + �di2i [N11]

Radix Conversion of 2's complement numbers.

Decimal to 2's Complement Conversion

Converting a positive decimal number to 2's complement form is identical to converting itto an unsigned binary number, except that you must be sure to include a leadingzero.

Example 14: Represent +1910 in 2's complement notation

Page 16: Introduction to Computer Architecture Data Representationfaculty.msmc.edu/christen/comp_ops-mgmt/data_representation.pdf · Introduction to Computer Architecture Data Representation

Introduction to Computer Architecture Data Representation

Data RepresentationNTC 10/26/04 16

Value ofbit n-1

Value ofremaining bits

Example: +19

Example: -19

Unsigned dn-12n-1 �dir

i 010011 n/a

Sign &Magnitude

sign �diri 010011 110011

2's Complement -dn-12n-1 �dir

i 010011 101101

Table TN2.

+1910 = 0100112 , not 100112 (which would be - 13)

To convert a negative decimal number to 2's complement form

a. Convert the number to binary, assuming it is positive, as above;b. Use the complement-and-add procedure to convert the positive 2's

complement number into a negative 2's complement number.

Example 15: Represent -1910 in 2's complement notation.

a. +19 = 010011 (remember to include the leading 0!)b. complement and add: 101100 + 1 = 1011012

2's Complement to Decimal Conversion

To convert a binary 2's complement number (regardless of sign) to decimal, use [N11]. That is, sum up the weights of all the positions containing a one, except that the weightof the most significant bit is negative. Example:

101101 = -25 + 23 + 22 + 20 = -32 + 8 + 4 + 1 = -32 + 13 = - 1910

Table TN2 summarizes these number formats and includes the previous examples. Notice that these formats differ only in the interpretation of the most significant bitposition (2n-1), and that positive numbers have the same representation regardless offormat (although the leading 0 is not required if the number is known to be in unsignedformat.)

Page 17: Introduction to Computer Architecture Data Representationfaculty.msmc.edu/christen/comp_ops-mgmt/data_representation.pdf · Introduction to Computer Architecture Data Representation

Introduction to Computer Architecture Data Representation

Data RepresentationNTC 10/26/04 17

Practice problems - 2's Complement Numbers:

1. Given the binary number 11011011, what is its decimal value if it is aa. unsigned binary number (Ans: 219)b. sign and magnitude number (Ans: -91)c. 2's complement number (Ans: -37)

2. Convert +24 into a 2's complement number (Ans: 011000 Note theleading zero!)

3. Convert -24 into a 2's complement number (Ans: 101000)

4. Convert -1 into a 2's complement number (assume a 4-bit result)(Ans: 1111)

Note that -1 is 11111...2 regardless of the size of the register. Note alsothat any positive or negative 2's complement number can have itsleading digit propagated to the left without changing the number.

5. What is the minimum (maximum negative) number which can berepresented using a 6-bit 2's Complement representation? What is themaximum (positive) number?

6. What is the minimum (maximum negative) number which can berepresented using a n-bit 2's Complement representation? What is themaximum (positive) number?

Convert, if possible, the following decimal numbers to 2's complementassuming an 8-bit binary representation for all.

8. 1 11. -1 14. 729. 127 12. -127 15. -10510. 255 13 -255 16. -6

Convert the following 2's complement numbers to decimal.

17. 1111 20. 1111000018. 001101 21. 1111111119. 0000000 22. 01

Page 18: Introduction to Computer Architecture Data Representationfaculty.msmc.edu/christen/comp_ops-mgmt/data_representation.pdf · Introduction to Computer Architecture Data Representation

Introduction to Computer Architecture Data Representation

1In practice, the bias is general chosen to be 1 less then the values presentedhere. For instance, as we will see, the IEEE single-precision floating point specification,which uses a biased 8 bit number for the exponent representation uses a bias of 127,not 128.

Data RepresentationNTC 10/26/04 18

binary unsigned Bias(3)000 0 -3001 1 -2010 2 -1011 3 0100 4 1101 5 2110 6 3111 7 4

• Biased numbers - It is sometimes desirable (we’ll see an example in a bit) tohave the range of negative and positive numbers by ‘monotonically’ increasing -that is, each successive number is determined by adding one to the previousnumber. Basically, we take our numerical sequence, and ‘shift’ the meanings ofthe combinations of ones and zero so that some of the combinations are negativein interpretation.

For example, using three bit binary numbers:

Notice that the decimal values have been ‘shifted’ down (or ‘biased’ by) 3positions, eliminating the values 5 thru 7 and inserting the values -3 thru -1.

The value of the bias is usually chosen to be roughly one half of the total range ofnumbers available, which is determined by the number of bits allocated to thenumber. In the above example, three bits give a total of eight numbers, 0 thru 7;we would generally choose a bias of three or four, depending on whether wewanted more positive or negative numbers. In general, for the purposes of thistext, choose a bias as follows:

1. Given the maximum number of binary bits, n, choose a bias1 = 2n/2 = 2n-1

If n = 8, the bias should be 128 ( = 28/2 = 256/2 = 128 = 28-1 = 27

If n = 16, the bias should be 216/2 = 32K

2. Given a negative decimal number, assume a bias equal to the minimum power

Page 19: Introduction to Computer Architecture Data Representationfaculty.msmc.edu/christen/comp_ops-mgmt/data_representation.pdf · Introduction to Computer Architecture Data Representation

Introduction to Computer Architecture Data Representation

Data RepresentationNTC 10/26/04 19

of two greater than the absolute value of the negative decimal number. Also, if2m is the bias, then the number of bits in the biased representation must be m+1. This is because, if the bias is 2m, then the range of numbers to be represented is -2m-1 thru +2m for a total range of 2 x 2m = 2m+1. This requires m+1 bits in thebinary biased representation.

If the negative decimal number is -23 assume a bias equal to 32. Thenumber of bits in the biased representation will be 6.If the negative decimal number is -129 assume a bias equal to 256. Thenumber of bits in the biased representation will be 9.

Converting Decimal numbers to Biased binary numbers

• Add the bias to the decimal number• convert the resulting decimal number to binary

Examples:

16. Assume a five-bit biased binary representation. What bias would you use? (Ans. 15 or 16, usually 15).

17. Using this bias, convert the decimal numbers 0, 6, and -12 to biased format.

Ans:0 + 15 = 15 = 0111126 + 15 = 21 = 101012-12 + 15 = 3 = 000112

18. Convert -49 to biased binary representation.

Choose a bias equal to 2m such that 2m > 49. In this case, the bias = 64.Then

a. Add the bias: -49+64 = 15b. Convert the result to binary: 1510 = 00011112

Notice that, since 2m = 64, m = 6 and the binary representation mustcontain m+1 bit positions (7 in this case).

Converting biased binary numbers to decimal

1. Convert the biased binary number to decimal

Page 20: Introduction to Computer Architecture Data Representationfaculty.msmc.edu/christen/comp_ops-mgmt/data_representation.pdf · Introduction to Computer Architecture Data Representation

Introduction to Computer Architecture Data Representation

Data RepresentationNTC 10/26/04 20

2. Subtract the bias from the resulting decimal number

Examples:

19. Convert the biased binary number 111111 to decimal

Since there are 6 binary digits, assume the bias is 26/2 = 25 =321. Convert to decimal: 1111112 = 63102. Subtract the bias: 63 - 32 = 31.

20. Convert the biased binary number 00101011 to decimal.

Since there are 8 bits, assume a bias of 1281. 001010112 = 43102. 43 - 128 = -85

21. Convert 0001 to decimal.

Bias = 80001 = 10 - 8 = -710

Page 21: Introduction to Computer Architecture Data Representationfaculty.msmc.edu/christen/comp_ops-mgmt/data_representation.pdf · Introduction to Computer Architecture Data Representation

Introduction to Computer Architecture Data Representation

Data RepresentationNTC 10/26/04 21

Practice Problems - Biased Binary Numbers

1. What bias should you choose for biased binary representations with each ofthe following number of bits?

a. 8 bits b. 2 bits c. 24 bits d. 13 bits

2. What bias should you choose for biased binary representation of each of thefollowing decimal numbers?

a. -10 b. -1013 c. -342 d. +75

3. Convert the following decimal numbers to the appropriate biased binary format.

a. -34 b. -12 c. +245 d. -129

4. Convert the following biased binary numbers to decimal

a. 101 b. 00011 c. 111111 d. 0000

Table TN3 shows the decimal values associated with the binary numbers 0000 through1111 in each of the binary number representations discussed.

Page 22: Introduction to Computer Architecture Data Representationfaculty.msmc.edu/christen/comp_ops-mgmt/data_representation.pdf · Introduction to Computer Architecture Data Representation

Introduction to Computer Architecture Data Representation

Data RepresentationNTC 10/26/04 22

UNSIGNEDBINARY

UNSIGNEDDECIMAL

SIGN &MAGNI-TUDE

2'SCOMPL-EMENT

BIAS(8) BIAS(7)

0000 0 0 0 -8 -7 0001 1 1 1 -7 -6 0010 2 2 2 -6 -5 0011 3 3 3 -5 -4 0100 4 4 4 -4 -3 0101 5 5 5 -3 -2 0110 6 6 6 -2 -1 0111 7 7 7 -1 0 1000 8 -0 -8 0 1 1001 9 -1 -7 1 2 1010 10 -2 -6 2 3 1011 11 -3 -5 3 4 1100 12 -4 -4 4 5 1101 13 -5 -3 5 6 1110 14 -6 -2 6 7 1111 15 -7 -1 7 8

Table TN3.

Note:

The above representations of binary numbers seem to refer to only integers. However,a ‘binary point’ (as opposed to a decimal point) could be assumed to be anywhere in theregister holding the number. If we have an eight bit (1 byte) register containing the bits10100111, it might, depending on the application, be interpreted to contain

101001112 = 16710, or

1010011.12 = 83.510, or

1010.01112 = 10.437510

These are called Fixed Point numbers, in contrast to floating point numbers which will bediscussed shortly.

Note that shifting a binary number left (and inserting a zero into the least significantdigit) is the same as multiplying by 2. Compare this to adding a low order zero to adecimal number. Shifting right performs the DIV 2 operation, since the least significant

Page 23: Introduction to Computer Architecture Data Representationfaculty.msmc.edu/christen/comp_ops-mgmt/data_representation.pdf · Introduction to Computer Architecture Data Representation

Introduction to Computer Architecture Data Representation

Data RepresentationNTC 10/26/04 23

bits are shifted out of the register and are lost.

0110112 = 2710 1101102 = 5410 0110112 = 2710

BCD numbers.

In some applications, such as in the financial industry, the errors that can creep in due toconverting numbers back and forth between decimal and binary is unacceptable. Forthese applications it is common to represent decimal numbers in format called ‘BinaryCoded Decimal’, or BCD. In this format, each decimal digit is converted to its binaryequivalent. Thus, each decimal digit uses 4 bits, and only the binary values 0000 thru1001 are used, as in the following table.

0000 0 1000 80001 1 1001 90010 2 1010 unused0011 3 1011 unused0100 4 1100 unused0101 5 1101 unused0110 6 1110 unused0111 7 1111 unused

Since each decimal digit uses 4-bits, a byte can only hold two digits. A 5 digit decimalnumber uses 2 ½ bytes. It is common to allocate space for an odd number of digits; thisleaves ½ of a byte left over for the sign so that BCD numbers often are expressed insign & magnitude format. (It is also possible to use 10's complement or 9's complementformats.)

Examples:

21. 9410 = 1001 0100BCD

22. 2810 = 0010 1000BCD

23. -9210 = 1111 1001 0010BCD

The above examples are more properly called Packed BCD numbers because two digitsare ‘packed’ into a single byte. This format is usually used for storing BCD numbers, but

Page 24: Introduction to Computer Architecture Data Representationfaculty.msmc.edu/christen/comp_ops-mgmt/data_representation.pdf · Introduction to Computer Architecture Data Representation

Introduction to Computer Architecture Data Representation

Data RepresentationNTC 10/26/04 24

for actually performing arithmetic an unpacked BCD format is generally used. Thisformat devotes an entire byte to each decimal digit, with the high order four bits equal to0's, except for the first byte, which might indicate the sign of the number in sign &magnitude notation

Examples:

24. 9410 = 00001001 00000100BCD

25. 2810 = 00000010 00001000BCD

26. -9210 = 11111001 00000010BCD

Practice Problems - BCD format

1. Express the following Decimal numbers in packed BCD format:

a. 0 b. 392 c. 59138 d. -3

2. Express the following Decimal numbers in unpacked BCD format:

a. 1 b. 201 c. 99 d. -999

3. What decimal number, if any, is represented by each of the following packedBCD numbers?

a. 0000 0111 0100 0000 c. 0000 0000 0000 1001b. 1111 0111 0110 0111 d. 0000 1001 1100 0001

4. What is the maximum unsigned decimal number which can be represented in

a. 4 bytes unsigned packed BCD notation?b. 12 byte unsigned unpacked BCD notation?

Floating point numbers

Floating point numbers are used wherever very large or very small numbers need to bemanipulated, such as in cosmological physics, or quantum physics, or other scientificdisciplines. This notation is also called scientific notation, or engineering notation.

Page 25: Introduction to Computer Architecture Data Representationfaculty.msmc.edu/christen/comp_ops-mgmt/data_representation.pdf · Introduction to Computer Architecture Data Representation

Introduction to Computer Architecture Data Representation

Data RepresentationNTC 10/26/04 25

In general usage, a floating point number is represented by a fraction multiplied by thebase raised to a power. For instance, given the fixed point number

3456.231,

this could be represented in scientific (floating point) notation as

3.456231 x 103 [N12]

It could also have been represented by 34.56231 x 102 or 3456231.0 x 10-3. It isconventional, however, to ‘normalize’ a number so that there is just one non-zero digit tothe left of the decimal point. Hence, [N12] is the ‘correct’ form of this number. Note thatboth the number itself as well as the exponent may be negative. E.g. -3.4 x 10-53

Nomenclature: The normalized digits are referred to as the ‘mantissa’ (I have takensome liberties here, as usually the mantissa only refers to the digits to the right of thedecimal point). The exponent is called the ‘exponent’.

The advantage of this representation is the range of numbers that can be represented. Remember that an n-digit decimal integer is restricted to the range 0 through 10n-1. (e.g.a 3 digit number goes from 0 through 999). If the n digits are all fractional digits, thenumbers representable are 0 through 10n-1/10n = 1-10-n. (Note that only numbers with nsignificant digits can be represented, so the entire continuum of fractions in this rangecan not be represented. A three digit fraction can represent .000 through .999, but not,say, .9985). In floating point notation, in addition to the number of significant digits ofthe mantissa, the decimal point can be placed anywhere in the allowable range of theexponent.

In a computer system, the exponent as well as the mantissa is held in a storage locationor register with limited extent. Let’s assume that (still considering decimal numbers) themantissa is given by 4 digits and the exponent is given 2 digits. Then the numbers thatcan be represented in this format (ignoring the signs for the time being are)

0.000 through 9.999 for the mantissa, and00 through 99 for the exponent.

The smallest number (ignoring signs) is 0.000 x 100 and the largest is 9.999 x 1099. If wesketch out the numbers on the real line, we see that there are gaps where non-representable numbers exist. For instance, none of the numbers between 1.000 x 102

and 1.001 x 102 can be represented. Even more interesting is the fact that none of thenumbers immediately following 0.000 x 100 (zero) are representable, such as 0.0001, sothe first gap is right at the origin of the real line. For each exponent, the gaps are

Page 26: Introduction to Computer Architecture Data Representationfaculty.msmc.edu/christen/comp_ops-mgmt/data_representation.pdf · Introduction to Computer Architecture Data Representation

Introduction to Computer Architecture Data Representation

Data RepresentationNTC 10/26/04 26

uniformly spaced and of uniform size, but as the exponents increase the gaps get bigger. In general, every individual number is separated by from every other one by a gap. Additionally, every exponent range is separated from each other by a gap. As numbersmove farther from zero, these gaps get larger.

Example: consider 1.000 x 100 and 1.001 x 100; they differ by .001. But1.000 x 102 and 1.001 x 102 differ by .001 x 102 = .1

We should expect something like this since it should be obvious that in a finite numberof bits only a finite number of numbers can be represented, despite the fact that in anyrange of real numbers there are an infinite number of them.

We will now discuss the actual representation of floating point numbers in a computersystem - binary floating point numbers and how they are stored in hardware registers.

Consider the number 10110.01012 = 1.01100101 x 24. To implement this in computerhardware we need to specify the following:

1. The number of bits allocated to the mantissa2. The number of bits allocated to the exponent3. How the sign of the number will be represented4. How the sign of the exponent will be represented.

In general, all of this information is kept in a single register, divided up into sections,called fields:

Sign Exponent Mantissa

The sign shown is a single bit and refers to the sign of the entire number (or,equivalently, of the mantissa.)

The exponent is in biased notation. The reason for this is as follows: consider twodecimal floating point numbers which you wish to add together, say 3.2 x 103 and 4.0 x105. Before they can be added, they must be made to have the same exponent. (i.e.we need to align the decimal points). We usually choose the number with the smallerexponent and increase it to match the other exponent, moving the decimal point to theleft a corresponding number of positions. (Moving the decimal point left is accomplishedin practice by shifting the fraction to the right; in this process some digits are lost as theyare shifted off the right end of the register - the least significant ones.) Therefore, weneed to easily be able to increase one or the other of the exponents by a (usually) smallamount, regardless of whether it is positive or negative, and even if the sign changesduring the process, as it might. See Table TN3 to see that, of the different number

Page 27: Introduction to Computer Architecture Data Representationfaculty.msmc.edu/christen/comp_ops-mgmt/data_representation.pdf · Introduction to Computer Architecture Data Representation

Introduction to Computer Architecture Data Representation

Data RepresentationNTC 10/26/04 27

representations, only the biased form provides this capability.

The mantissa, or fraction, is the normalized form of the given digits, or as many of themas will fit in the allocated space. Remember that a normalized number, in ourdiscussion, has a single non-zero digit to the left of the binary point. Since there is onlyone possible non-zero value, one, some savings are realized by not bothering to actuallywaste a bit on that digit - it is always assumed, or implied, that a 1 precedes the rest ofthe fraction. This allows an extra bit of significance to be maintained in the mantissafield of the register.

A typical floating point number in this format would look like this:

0 011001 01001111000000000 (or 329E00 in hexadecimal notation)

where the sign is +, the exponent is 011001 (we haven’t specified the bias) and themantissa is 1.01001111000000000 (note that we have added the implied one to the leftof the binary point.) Notice that this format does not allow you to represent a value ofzero! If we are given

0 000000 00000000000000000

we should interpret this as an exponent of 000000 but a mantissa of1.00000000000000000. It is usual, however, to treat zero as a special case: When allthe bits in the representation are 0's, as in this last example, then the number isassumed to be zero and no implied 1 is included when interpreting the number.

Although computer designers can arbitrarily choose the size of the register to be used aswell as the size of the fields, most modern systems follow the standards established bythe IEEE (Institute of Electrical and Electronic Engineers). This standard provides twoformats, a single precision format and a double precision format. They are summarizedin table TN4. When all of the bits in the format are 0's, the number is assumed to bezero.

Page 28: Introduction to Computer Architecture Data Representationfaculty.msmc.edu/christen/comp_ops-mgmt/data_representation.pdf · Introduction to Computer Architecture Data Representation

Introduction to Computer Architecture Data Representation

Data RepresentationNTC 10/26/04 28

Feature Single Precision Format Double Precision Format

Register Length 32 bits 64 bits

Mantissa 23 + 1 implied 52 + 1 implied

Exponent, Bias 8 bits, 127 11 bits, 1023

Table. TN4. IEEE Floating Point Standard

Binary Addition & Subtraction

Unsigned and Sign & Magnitude numbers

Addition and subtraction of unsigned or sign & magnitude binary numbers ‘by hand’proceeds exactly as with decimal numbers. (In fact this is true for any radix numbers)

Examples - Addition (unsigned)

30. 31. 32.

01100102 315 A3D16 + 11001102 + 145 + F216 100110002 1005 B2F16

If the result has more digits than can fit in the assigned storage word (i.e, there is a carryout of the last bit that can be held), then an overflow has occurred. Usually a computererror handler (program) is invoked to deal with the problem.

Examples - Subtraction (unsigned)

33. 34. 35.

01100102 145 A3D16 - 00101102 - 315 - F216 00111002 -125 94B16

As in decimal, if the numbers are of different signs:

1. when adding you subtract the smaller from the larger and assign the sign of the

Page 29: Introduction to Computer Architecture Data Representationfaculty.msmc.edu/christen/comp_ops-mgmt/data_representation.pdf · Introduction to Computer Architecture Data Representation

Introduction to Computer Architecture Data Representation

Data RepresentationNTC 10/26/04 29

larger number; 2. when subtracting, you must keep track of whether the negative number is the

subtrahend (in which case you change its sign and add, getting a positive result)or the minuend (in which case you add without changing any signs, and the resultis negative.)

Practice Problems - Unsigned Arithmetic

1. Do the following binary additions

a. 1001101 b. 11111 c. 101010 + 1011011 + 00001 +010101 --------------- ------------- --------------

2. Do the following binary subtractions

a. 100000 b. 110010 c. 1111111 - 000001 - 010001 - 1010101 -------------- -------------- -----------------

2's complement numbers

When the numbers are in 2's complement form, there is no need to keep track ofwhether the numbers have different signs and, if so, which is bigger. In the case ofsubtraction, however, the subtrahend must have its sign reversed; the numbers are thensimply added normally. This sign reversal is accomplished by the complement and addprocedure we’ve already discussed.

Examples - Addition (2's complement in 5-bit registers)

36. 01001 37. 01001 38. 01001 39. 10101 + 00011 + 01001 + 11111 + 11010 01100 10010 1 01000 1 01111

In 36, both numbers are positive and the expected result is achieved (in decimal, 9 + 3 =12).

In37, both numbers are again positive, but the result is negative (note the leading 1)! Itappears that 9 + 9 = - 14. This is considered an overflow situation, even though no

Page 30: Introduction to Computer Architecture Data Representationfaculty.msmc.edu/christen/comp_ops-mgmt/data_representation.pdf · Introduction to Computer Architecture Data Representation

Introduction to Computer Architecture Data Representation

Data RepresentationNTC 10/26/04 30

carry actually overflowed out of the most significant bit. In a computer, hardware isimplemented to detect such overflows, and they do so as follows.

Consider the two most significant bit positions, bn-1 and bn-2. Call the carries out of thesecn-1, cn-2, where cn-1 is the bit that actually overflows into the next (non-existent) bit bn. Then an overflow is detected whenever cn-1 <> cn-2. In example b), cn-1 = 0 (there is nocarry) and cn-2 is 1 (there is a carry); Since they are different, an overflow is detected,and the answer is known to be incorrect.

In 38, one number is positive and the other is negative. There appears to be anoverflow, but in fact the answer (ignoring cn-1) is correct (9 + (- 1) = 8). Notice that theconditions for detecting an actual overflow as described above do not exist: both cn-1(c5)and cn-2 (c4) are 1; since they are the same, there is no overflow detected, and theanswer, ignoring c5, is correct.

In 39, both numbers are negative. The answer appears to be +15, an unlikely resultwhen adding two negative numbers. However, since c4 and c3 are different (c3=0, c4=1),we know that an overflow has occurred, and therefore the answer is incorrect.

Examples - subtraction (2's complement in 5-bit registers)

40. 01001 41. 11001 42. 01001 43. 10101 - 00011 - 01010 - 11111 - 11010

In each of the above cases, the subtrahend must be converted to its 2's complementvalue and then normal addition is done.

01001 11001 01001 10101 + 11101 + 10110 + 00001 + 00110 1 00110 1 01111 01010 11011

Checking for overflows in each of these examples shows one only in example 41. Wecan conclude, then, that all the results except41 are correct.

Page 31: Introduction to Computer Architecture Data Representationfaculty.msmc.edu/christen/comp_ops-mgmt/data_representation.pdf · Introduction to Computer Architecture Data Representation

Introduction to Computer Architecture Data Representation

Data RepresentationNTC 10/26/04 31

Practice Problems - 2's Complement Arithmetic

1. Do the following 2's complement additions. In all cases assume results areplaced in an 8-bit register. For each problem, indicate whether the additionresulted in an overflow.

a. 01100001 b. 01100001 c. 01101111 d. 1111000000001111 00101110 11111111 00010000------------- ------------- -------------- -------------

2. Do the following 2's complement subtractions. In all cases assume results areplaced in an 8-bit register. For each problem, indicate whether the subtractionresulted in an overflow.

a. 01010100 b. 00000110 c. 10000000 d. 11100011 - 00101001 - 10000011 - 00000001 - 11100011 ----------------- ----------------- ----------------- -----------------

BCD numbers

Consider the addition of the two unsigned BCD numbers 29 and 37.

44. 0010 1001 + 0011 0111

0110 0000

The answer shown, 60, is clearly incorrect. It looks like it might be correct, since bothdigits are less than 9. Consider another example, 16 and 69.

45. 0001 0110 + 0110 1001

0111 1111

This answer is clearly wrong since one of the digits is not a legal decimal digit. Youmight ask, why are we doing binary arithmetic on BCD numbers? Shouldn’t we bebuilding hardware to do BCD arithmetic. The answer is that, yes we could buildhardware to do this (some IBM System/360 models did this), but it is very wasteful todesign and build special (BCD) hardware that duplicates the function of other (binary)hardware, if the binary hardware can be modified inexpensively to do the same job.

Page 32: Introduction to Computer Architecture Data Representationfaculty.msmc.edu/christen/comp_ops-mgmt/data_representation.pdf · Introduction to Computer Architecture Data Representation

Introduction to Computer Architecture Data Representation

Data RepresentationNTC 10/26/04 32

In the case of BCD arithmetic, this is easy to do, and it is done using something calledexcess-3 coding (or excess-6 coding, or both). Excess-3 coding simply recodes everydigit to be used in the arithmetic calculation by adding 3 to it, as in Table TN5

Decimal BCD Excess-3

0123456789

0000000100100011010001010110011110001001

0011010001010110011110001001101010111100

Table TN5. Excess-3 Coding

Now consider example 44. Rewriting

0010 1001 (29) + 0011 0111 (37)

in excess-3 coding results in

0101 1100 + 0110 1010giving an answer of 1100 0110 (C6)

This still isn’t correct, but notice: the low order nybble does have the correct answer (6),and the high order nybble differs from the correct answer by 6. In fact, we should expectboth digits to be 6 more than they should be since we added 3 to all of the digits beforewe started. As we can see, we need to subtract 6 from the first digit and leave thesecond digit alone. The reason is, again, involved with carries, or overflows, out of thedigits. The general rule is,

1. if the digit has a carry out of it, it will have the correct answer; 2. if there is no carry out of a digit, then we must subtract six from the digit to correct

it.

Page 33: Introduction to Computer Architecture Data Representationfaculty.msmc.edu/christen/comp_ops-mgmt/data_representation.pdf · Introduction to Computer Architecture Data Representation

Introduction to Computer Architecture Data Representation

Data RepresentationNTC 10/26/04 33

Applying this principle to the current example, the first digit had no carry out, while thesecond digit did. The adjusted result, after subtracting six from the first digit, is 01100110 (66). Let’s apply these rules to example 45..

0001 0110 (16) + 0110 1001 (69)

Adding 3 to every digit gives

0100 1001 + 1001 1100

which, adding, gives 1110 0101

The carries in this case happen to be the same as in the previous example, so we needto subtract 6 from the first digit, giving the result 1000 0101 (85), the correct answer.

Now consider the subtraction of two BCD numbers. We will assume unsigned binarysubtraction algorithms, rather than 9's complement or 10's complement numbers, as issometimes done. Let’s subtract 23 from 85

1000 0101 (85) - 0010 0011 (-23)

We again convert these numbers into excess-3 notation:

1011 1000 - 0101 0110

Applying the rules of unsigned binary subtraction gives a result of 0110 0010 (62) whichis the correct answer, without any adjustment being necessary! We should expect thisto occur, since subtracting removes all the excess-3 coding automatically. In this case,(5+3) - (3+3) = 5+3- 3-3 = 5-3 and (8+3) - (2+3) = 8+3-2-3 = 8-2. Of course, as youmight expect, we were just lucky. Let’s take another example, say 81 - 23.

1000 0001 becomes 1011 0100 0010 0011 0101 0110

and subtracting gives 0101 1110. If we subtract 6 from the second digit, the answer isnow correct: 0101 1000. Thus, a similar rule applies for subtraction as we used foraddition, only now instead of looking for a carry out of a digit, we are looking for a borrowinto a digit. If such a borrow exists, then subtracting 6 will correct the digit.

Page 34: Introduction to Computer Architecture Data Representationfaculty.msmc.edu/christen/comp_ops-mgmt/data_representation.pdf · Introduction to Computer Architecture Data Representation

Introduction to Computer Architecture Data Representation

Data RepresentationNTC 10/26/04 34

It is important to note that even though the answer may consist of all legal digits,adjustments may still be necessary. One last example addition example

46. 0001 0000 (10) ==excess-3==> 0100 0011 + 0010 0011 (23) 0101 0110

-------------- 1001 1001

Although the answer, 99, contains valid decimal digits, it is incorrect. The correctanswer is 33, and we know this because neither digit had a carry out, requiring that wesubtract 6 from each of them.

Practice Problems - BCD Arithmetic

1. Do the following BCD additions. For each problem, state which digit(s), if any,required adjustment.

a. 00100101 b. 00010000 c. 01010111 + 01000101 + 00100110 + 10000101

----------------- ----------------- ------------------

2. Do the following BCD subtractions. For each problem, state which digit(s), ifany, required adjustment.

a. 10010010 b. 00111000 d. 10011001 - 00010011 - 00000110 - 01100100 ---------------- ------------------ ------------------

Page 35: Introduction to Computer Architecture Data Representationfaculty.msmc.edu/christen/comp_ops-mgmt/data_representation.pdf · Introduction to Computer Architecture Data Representation

Introduction to Computer Architecture Data Representation

Data RepresentationNTC 10/26/04 35

Practice Problems - Binary Floating Point Numbers

1. Using the floating point register format given above for the examples, show theregister contents for the following binary floating point numbers:

1. 101.1101 x 23 b. -.001111 x 28 c. 11.0011 x2-6

2. What is the minimum number of exponent bits required to accommodate thefollowing binary numbers (review the section on biased binary numbers, ifnecessary.)

1. 1.0 x 2324 b. 1110010.00111 x 2-133

3. What is the binary floating point number (in the form b.bbb... x 2e) representedby each of the following FLP register contents

1. 0 01110 010010010 (five bit exponent, bias = 15)2. 1 11111111 00000000 (eight bit exponent, bias = 127)3. 3B1CFF16 (six bit exponent, bias = 31)

4. Express the following binary floating point number in IEEE floating point format

11011110.0010010 x 2-75

5. The following hex number represents the contents of an IEEE floating pointregister. Express this in normalized binary floating point form ( b.bbb... x 2e)

9F380000

Answers to Practice Problems

Practice Problems - Integer Number System Conversions

1. Convert the decimal integer 42710 into binary. 1101010112

2. Convert 1001111012 to a decimal integer.

31710

Page 36: Introduction to Computer Architecture Data Representationfaculty.msmc.edu/christen/comp_ops-mgmt/data_representation.pdf · Introduction to Computer Architecture Data Representation

Introduction to Computer Architecture Data Representation

Data RepresentationNTC 10/26/04 36

3. Convert the following decimal integers to binary:

a. 2 = 102 c. 27 = 110112 e. 128 = 100000002 g. 31 = 111112b. 245 = 111101012 d. 127 = 11111112 f. 17 = 100012 h. 257 = 1000000012

Practice Problems - Fraction Number System Conversion

1. Convert the following fractions to binary:

a. .875 = .1112 b. .2 = .00110011....2 c. .71 = .101101011....2

2. Convert the following fractions from bnary to decimal:

a. .011012 = 13/32 b. .000012 = 1/32

Practice problems - 2's Complement Numbers:

3. Given the binary number 11011011, what is its decimal value if it is a1. unsigned binary number Ans: 2192. sign and complement number Ans: -913. 2's complement number Ans: -37

4. Convert +24 into a 2's complement number Ans: 011000 Note the leading zero!

5. Convert -24 into a 2's complement number Ans: 1010000

6. Convert -1 into a 2's complement number (assume a 4-bit result)Ans: 1111

Note that -1 is 11111...2 regardless of the size of the register. Note alsothat any positive or negative 2's complement number can have its leading digit propagated to the left without changing the number.

5. What is the minimum (maximum negative) number which can berepresented using a 6-bit 2's Complement representation? What is themaximum (positive) number?

Ans: +31, -32

6. What is the minimum (maximum negative) number which can be

Page 37: Introduction to Computer Architecture Data Representationfaculty.msmc.edu/christen/comp_ops-mgmt/data_representation.pdf · Introduction to Computer Architecture Data Representation

Introduction to Computer Architecture Data Representation

Data RepresentationNTC 10/26/04 37

represented using a n-bit 2's Complement representation?

Ans.: -2n-1

What is the maximum (positive) number? Ans..: 2n-1-1

Convert, if possible, the following decimal numbers to 2's complement assumingan 8-bit binary representation for all.

8. 1 = 00000001 11. -1 = 11111111 14. 72 = 010010009. 127 = 01111111 12. -127 = 10000001 15. -105 = 1001011110. 255 = N/G 13 -255 (N/G 16. -6 = 11111010

Convert the following 2's complement numbers to decimal.

17. 1111 = -1 20. 11110000 = -1618. 001101 = 13 21. 11111111 = -119. 0000000 = 0 22. 01 = 1

Practice Problems - Biased Binary Numbers

1. What bias should you choose for biased binary representations with each of thefollowing number of bits?

a. 8 bits b. 2 bits c. 24 bits d. 13 bitsBias = 128 2 8M 4K

2. What bias should you choose for biased binary representation of each of thefollowing decimal numbers?

a. -10 b. -1013 c. -342 d. +75Bias = 16 1024 512 128

3. Convert the following decimal numbers to the appropriate biased binary format.

a. -34 b. -12 c. +245 d. -129Ans.: 011110 00100 111110101 01111111

4. Convert the following biased binary numbers to decimal

a. 101 b. 00011 c. 111111 d. 0000

Page 38: Introduction to Computer Architecture Data Representationfaculty.msmc.edu/christen/comp_ops-mgmt/data_representation.pdf · Introduction to Computer Architecture Data Representation

Introduction to Computer Architecture Data Representation

Data RepresentationNTC 10/26/04 38

Ans: 1 -13 31 -8

Practice Problems - BCD format

1. Express the following Decimal numbers in packed BCD format:

a. 0 = 00000000 b. 392 = 00000011 10010010c. 59138 = 00000101 10010001 00111000 d. -3 = 11110011

2. Express the following Decimal numbers in unpacked BCD format:

a. 1 = 00000001 b. 201 = 00000010 00000000 00000001c. 99 = 00001001 00001001 d. -999 = 11111001 00001001 00001001

3. What decimal number, if any, is represented by each of the following packedBCD numbers?

a. 0000 0111 0100 0000 c. 0000 0000 0000 1001= 740 = 9

b. 1111 0111 0110 0111 d. 0000 1001 1100 0001= -767 N/G (1100 is not a valid BVD digit

4. What is the maximum unsigned decimal number which can be represented in

a. 4 bytes unsigned packed BCD notation? Ans.: 99999999b. 12 byte unsigned unpacked BCD notation? Ans.: 999999999999

Practice Problems - Binary Floating Point Numbers

1. Using the floating point register format given above for the examples, show theregister contents (in binary, not hex) for the following binary floating pointnumbers:

a. 101.1101 x 23 b. -.001111 x 28 c. 11.0011 x 2-6

Ans.: 0 1100 01110100000 1 1100 11100000000 0 0010 10011000000

2. What is the minimum number of exponent bits required to accommodate thefollowing binary numbers (review the section on biased binary numbers, ifnecessary.)

Page 39: Introduction to Computer Architecture Data Representationfaculty.msmc.edu/christen/comp_ops-mgmt/data_representation.pdf · Introduction to Computer Architecture Data Representation

Introduction to Computer Architecture Data Representation

Data RepresentationNTC 10/26/04 39

a. 1.0 x 2324 b. 1110010.00111 x 2-133

Ans.: 10 exponent bits 8 exponent bits

3. What is the binary floating point number (in the form b.bbb... x 2e) represented byeach of the following FLP register contents

a. 0 01110 010010010 (five bit exponent, bias = 15)1.010010010 x 2-1

b. 1 11111111 00000000 (eight bit exponent, bias = 127)-1.0 x 2128

c. 3B1CFF16 (six bit exponent, bias = 31)1.10001110011111111 x 2-2

4. Express the following binary floating point number in IEEE floating point format

11011110.0010010 x 2-75

0 00111011 1011110001001000000...

5. The following hex number represents the contents of an IEEE floating pointregister. Express this in normalized binary floating point form ( b.bbb... x 2e)

9F380000 = -1.01110 x 2-65

Page 40: Introduction to Computer Architecture Data Representationfaculty.msmc.edu/christen/comp_ops-mgmt/data_representation.pdf · Introduction to Computer Architecture Data Representation

Introduction to Computer Architecture Data Representation

Data RepresentationNTC 10/26/04 40