23
53 © 2015 Gilbert Ndjatou Chapter 3 DATA REPRESENTATION Binary codes are used to represent both characters and numbers inside computers. Moreover, the binary codes used to represent numbers must be consistent with the arithmetic operations on those numbers: by performing an arithmetic operation on the codes of two numbers, we must obtain the code of the result of that operation on the numbers. Also, by their nature, computers can only represent quantities having a finite number of bits. Therefore, only a finite number of integers can be represented inside a digital computer system. One of these integers is the largest value, and another one the smallest one. This chapter describes the different binary codes used to represent characters and signed integers. It also discusses the limits imposed by these representations, and examines arithmetic operations on these numbers, and how errors occur. 3.1 Character Representation The two major codes used to represent characters inside digital computers are the EBCDIC (extended binary-coded decimal information code) and the ASCII (american standard code for information interchange) codes. The EBCDIC code is used in IBM 360/370 mainframe systems, whereas the ASCII code is used in most computers, including computer systems based on Intel processors.

Chapter 3 DATA REPRESENTATION - Computer Science …cs.wpunj.edu/~ndjatou/CS2800-chapter3.pdf · Chapter 3 DATA REPRESENTATION Binary codes are used to represent both characters and

  • Upload
    lydung

  • View
    230

  • Download
    3

Embed Size (px)

Citation preview

Page 1: Chapter 3 DATA REPRESENTATION - Computer Science …cs.wpunj.edu/~ndjatou/CS2800-chapter3.pdf · Chapter 3 DATA REPRESENTATION Binary codes are used to represent both characters and

53© 2015 Gilbert Ndjatou

Chapter 3

DATA REPRESENTATION

Binary codes are used to represent both characters and numbers inside

computers. Moreover, the binary codes used to represent numbers must be

consistent with the arithmetic operations on those numbers: by performing an

arithmetic operation on the codes of two numbers, we must obtain the code

of the result of that operation on the numbers. Also, by their nature,

computers can only represent quantities having a finite number of bits.

Therefore, only a finite number of integers can be represented inside a digital

computer system. One of these integers is the largest value, and another one

the smallest one.

This chapter describes the different binary codes used to represent

characters and signed integers. It also discusses the limits imposed by these

representations, and examines arithmetic operations on these numbers, and

how errors occur.

3.1 Character Representation

The two major codes used to represent characters inside digital computers

are the EBCDIC (extended binary-coded decimal information code) and the

ASCII (american standard code for information interchange) codes. The

EBCDIC code is used in IBM 360/370 mainframe systems, whereas the

ASCII code is used in most computers, including computer systems based on

Intel processors.

Page 2: Chapter 3 DATA REPRESENTATION - Computer Science …cs.wpunj.edu/~ndjatou/CS2800-chapter3.pdf · Chapter 3 DATA REPRESENTATION Binary codes are used to represent both characters and

54

© 2015 Gilbert Ndjatou

With these codes, a character is represented by an 8-bit (two hexadecimal

digits) code, and a character string is represented by a sequence of bits

consisting of the codes of the individual characters in the string. For

example, the string "CS-280" is represented in ASCII by the sequence of

hexadecimal digits 43532D323830h, where 43h is the ASCII code of the

character ‘C’, 53h the code of ‘S’, 2Dh the code of ‘-’, 32h the code of ‘2',

38h the code of ‘8', and 30h the code of ‘0'.

Although the ASCII code is an 8-bit code, only the right-most seven bits

are used to encode information. It then follows that only 128 (2 ) characters7

can be encoded. Using all 8 bits to encode information makes it possible to

represent 256 (2 ) characters. This extension of the ASCII code is used on8

computers with Intel processors and is referred to as the Extended ASCII

code. The ASCII code is provided in appendix 1. It consists of the control

codes that are used as signals to control the different components of the

computer, and the codes for the printable characters (that means characters

that may be displayed on the screen or sent to the printer).

With only 128 (2 ) character codes for the ASCII code and 256 (2 )7 8

character codes for the Extended ASCII code, the requirements of many

languages such as the Japanese language could not be met. Therefore, certain

computer industry companies are suggesting a new 16-bit code called

Unicode. The 16-bit Unicode has 65,536 (2 ) character codes, making it16

possible to represent the alphabet of most languages of the world.

Exercise 3.1

Part I

1. What are the two major codes that are used to represent characters in most computers today?

2. ASCII stands for: ______________________________________________________________.

3. EBCDIC stands for: ____________________________________________________________.

4. What is the code that is used to represent characters on IBM PC and compatible computers?

5. How many characters are represented in ASCII code?

Page 3: Chapter 3 DATA REPRESENTATION - Computer Science …cs.wpunj.edu/~ndjatou/CS2800-chapter3.pdf · Chapter 3 DATA REPRESENTATION Binary codes are used to represent both characters and

55

© 2015 Gilbert Ndjatou

6. How many characters are represented in Extended ASCII code?

7. What is the difference between the ASCII code and the Extended ASCII code?

8. How many characters are represented in unicode?

Part II

Provide the ASCII code representation of each of the following sequences of characters:

a. CS-280 b. Mark’s tape c. 973-720-2649 d. Two_Flags

3.2 Numeric Data

Three types of numbers are represented inside most digital computer systems:

binary integers, binary-coded decimal (BCD), and floating-point numbers.

Binary integers use a fixed number of bits to represent signed and unsigned

integers. Binary-coded decimal numbers allow for a greater range of values

than binary integers, and for the representation of an implicit decimal point,

whereas floating-point numbers are used to approximate real numbers. This

chapter discusses the representation of binary integers.

Unsigned and signed binary integers are represented inside a digital

computer by a fixed number of bits. The number of bits used on most

computers are 8, 16, 32, and 64 bits.

Unsigned integers are used to represent zero and positive integer values

in base 2. For example the 8-bit unsigned integers represent integers values

in the range 0 to 511 (2 - 1 ) and the 16-bit unsigned integers represent8

integer values in the range 0 to 65,535 (2 - 1 ).16

The two encoding methods suggested for the representation of signed

integers are the sign-magnitude and the two's complement. But most

computers, including computer with Intel processors use the two’s

complement representation.

Page 4: Chapter 3 DATA REPRESENTATION - Computer Science …cs.wpunj.edu/~ndjatou/CS2800-chapter3.pdf · Chapter 3 DATA REPRESENTATION Binary codes are used to represent both characters and

56

© 2015 Gilbert Ndjatou

Sign-Magnitude Number Systems

In n-bit sign-magnitude number system, a signed integer is represented as:

n-2 n-3 2 1 0s b b ... b b b

n-2 n-3 2 1 0where b b ... b b b is the magnitude (absolute value) of the number in

base 2 and s is 0 if the number is positive, and 1 if it is negative. s is referred

to as the sign bit. This representation is illustrated in Example 3.1.

Example 3.1 Conversion of Decimal Integers to Sign-Magnitude Number Systems

Convert to 8-bit sign-magnitude number system the decimal integers 6, -10, 47, 127

and -127.

6 = 00000110 = 06h

-10 = 10001010 = 8Ah

47 = 00101111 = 2Fh

127 = 01111111 = 7Fh

-127 = 11111111 = FFh

Observe that 128 and -128 can not be represented in 8-bit sign-magnitude number

system. In general, the range of values that can be represented in n-bit sign magnitude is -

(2 - 1) to 2 - 1. This representation uses n-1 bits to represent the absolute value of ann-1 n-1

integer, with the largest absolute value being the decimal value 2 - 1. So, in 16-bit signn-1

magnitude, only the integer values in the range -2 +1 = -32,767 to 2 -1 = 32,767 can15 15

be represented.

The rules for sign-magnitude addition are the same that we use for decimal integer

addition:

Page 5: Chapter 3 DATA REPRESENTATION - Computer Science …cs.wpunj.edu/~ndjatou/CS2800-chapter3.pdf · Chapter 3 DATA REPRESENTATION Binary codes are used to represent both characters and

57

© 2015 Gilbert Ndjatou

1. If the signs of the two numbers are the same, add the two magnitudes,

and attach the common sign.

2. If the signs of the two numbers are different, then subtract the smaller

magnitude from the larger one to obtain the magnitude of the result.

The sign of the result is the sign of the number having the larger

magnitude.

So, in 8-bit sign-magnitude,

-4 + 6 is 10000100 + 00000110 = 00000010 = 2 and,

-6 + (-8) is 10000110 + 10001000 = 10001110 = -14.

Overflow in a Sign-Magnitude Number System

An overflow occurs when the result of a computation falls outside of the

range of a number system. In n-bit sign-magnitude, this is the case when the

magnitude of the result has more than n-1 bits. For example, the following

addition of 8-bit sign-magnitude integers generates an overflow condition:

01111111 + 00000001 = 00000000

The last carry does not affect the sign bit; it is lost and the result is

erroneously 00000000. Note that an overflow condition can not be generated

when both operands of an addition operation have opposite signs.

Disadvantages of the Sign-Magnitude Number System

There are two major drawbacks to the sign-magnitude representation of

integers: First, there are two representation of 0: +0 = 00000000 and -0

= 10000000. Second, the addition of integers requires either the addition or

the subtraction of the magnitudes, depending on the signs of the operands.

Therefore, in addition to providing a circuit that performs addition, the

computer manufacturer must also provide a circuit to perform subtractions.

However, this additional circuit is not necessary for other integer

representations such as the two’s complement representation described

bellow.

Page 6: Chapter 3 DATA REPRESENTATION - Computer Science …cs.wpunj.edu/~ndjatou/CS2800-chapter3.pdf · Chapter 3 DATA REPRESENTATION Binary codes are used to represent both characters and

58

© 2015 Gilbert Ndjatou

Exercise 3.2

1. Convert the following decimal integers to 8-bit sign-magnitude number system:

a. 56 b. -27 c. 18 d. -89

2. Perform the following operations on 8-bit sign-magnitude integers and also indicate whether or

not there is an overflow condition:

a. 0000 1010 + 0000 0110 b. 1000 1010 + 0000 0110

c. 0000 1010 + 1000 0110 d. 1110 1010 + 1010 0110

e. 0100 0101 + 01001 0010 f. 1001 0110 + 1101 1001

3. What are the two major disadvantages of the sign-magnitude number systems?

Two's Complement Binary Number Systems

In n-bit two’s complement binary number system, 2 n-bit codes are usedn

to represent positive and negative integers as follows:

! the binary representation of 2 using n bits is used to represent -2n-1 n-1

! the binary representation (using n bits) of 0 or any positive integer

less than 2 is used to represent that integern-1

! for each positive integer m less than 2 with n-bit code d, the n-bitn-1

code that represents -m is 2 - d.n

It follows from the above description that in n-bit two’s complement

binary number system, we have the following conditions:

a) only the positive integers less than 2 can be represented. Morever,n-1

a positive integer is represented by its binary representation using n

bits.

b) the smallest negative integer that can be represented is -2 . It isn-1

represented by the binary representation (using n bits) of 2 .n-1

Page 7: Chapter 3 DATA REPRESENTATION - Computer Science …cs.wpunj.edu/~ndjatou/CS2800-chapter3.pdf · Chapter 3 DATA REPRESENTATION Binary codes are used to represent both characters and

59

© 2015 Gilbert Ndjatou

c) if d is the n-bit code that represents a positive integer m, then the n-bit

code that represents -m is 2 - d.n

Table 3.1 illustrates the representations of 8 and 16-bit two’s complement

binary integers.

The range of negative and positive decimal integers that can be

represented in n-bit two’s complement number system is -2 to 2 - 1. Forn-1 n-1

example, in 8-bit two’s complement number system, the range of decimal

integers is -2 = - 128 to 2 -1 = 127, and in 16-bit the range of decimal7 7

integers is -2 = -32,768 to 2 -1 = 32,767.15 15

Computers with the Intel 8086 processor represent integers in two's

complement binary number system using 8, 16, and 32 bits. In these

computers, 8, 16, and 32-bit two's complement binary integers are usually

referred to as short integers, integers and long integers respectively, in

some high-level languages.

Exercise 3.3

Provide the 3-bit two’s complement binary representation of integers.

Taking the Two’s Complement

The two’s complement of an n-bit integer d is to 2 - d. For example, then

two’s complement of the 8-bit integer 00001011 is computed as follows:

1 0 0 0 0 0 0 0 0 ² 2 8

- 0 0 0 0 1 0 1 1 ² d

1 1 1 1 0 1 0 1 ² Two’s complement of d

Page 8: Chapter 3 DATA REPRESENTATION - Computer Science …cs.wpunj.edu/~ndjatou/CS2800-chapter3.pdf · Chapter 3 DATA REPRESENTATION Binary codes are used to represent both characters and

60

© 2015 Gilbert Ndjatou

Table 3.1 Range of 8-bit and 16-bit Two’s Complement Binary Integers

Decimal Integers 8-Bit Binary Integers 16-Bit Binary Integers

-2 = -32,768 800015

-2 +1= -32,767 800115

-2 +2= -32,766 800215

. . .

. . .

. . .

-2 -1= -129 - FF7F7

-2 = -128 80 FF807

-2 +1= -127 81 FF817

. . .

. . .

. . .

-2 FE = 100 - 2 FFFE = 10000h - 2

-1 FF = 100h - 1 FFFF = 10000h - 1

0 00 0000

1 01 0001

. . .

. . .

. . .

2 -1= 127 7F 007F7

2 = 128 - 00807

. . .

. . .

. . .

2 -1 = 32,767 . 7FFF15

Page 9: Chapter 3 DATA REPRESENTATION - Computer Science …cs.wpunj.edu/~ndjatou/CS2800-chapter3.pdf · Chapter 3 DATA REPRESENTATION Binary codes are used to represent both characters and

61

© 2015 Gilbert Ndjatou

Note that 2 - d = (2 - 1) - d + 1 and (2 - 1) is an n-bit integer consistingn n n

of 1's. Therefore, the two’s complement of the 8-bit integer above can also

be computed as follows:

1 1 1 1 1 1 1 1 ² 2 - 1 8

- 0 0 0 0 1 0 1 1 ² d

1 1 1 1 0 1 0 0

+ 1

1 1 1 1 0 1 0 1 ² Two’s complement of d

In hexadecimal, it is computed as follows:

FF

- 0B

F4

+ 1

F5

Observe that subtracting a bit value from 1 is the same thing as inverting

that bit value. That means, if it is 1, change it to 0; and if it is 0, change it to

1. The two’s complement of an n-bit integer may then be computed by

applying the following two rules, Rule 3.1.a and Rule 3.1.b. Rule 3.1.a

applies when the integer is in binary, and Rule 3.1.b applies when it is in

hexadecimal.

Rule 3.1. a To compute the two’s complement of an n-bit integer, do the

following:

1. Invert each bit value (the result is called the one’s

complement)

2. Add 1 to the one’s complement.

Page 10: Chapter 3 DATA REPRESENTATION - Computer Science …cs.wpunj.edu/~ndjatou/CS2800-chapter3.pdf · Chapter 3 DATA REPRESENTATION Binary codes are used to represent both characters and

62

© 2015 Gilbert Ndjatou

Rule 3.1. b To compute the two’s complement of an hexadecimal integer,

do the following:

1. Subtract each hexadecimal digit from F (the result is

called the one’s complement)

2. Add 1 to the one’s complement.

These rules are illustrated in Example 3.2. You may notice in this example

that the two’s complement of C6h is 3Ah and the two’s complement of 3Ah

is C6h. This is because, by taking the two’s complement twice, you obtain

the original number: 2 - (2 - d) = 2 - 2 + d = d. However, if d is then n n n

n-bit two’s complement binary code that represents -2 , then the two’sn-1

complement of d is d. For example, the two’s complement of 80h is 80h, and

the two’s complement of 8000h is 8000h.

Example 3.2 Computing the Two’s complement

Compute the two’s complement of the following binary/hexadecimal integers:

a. 0011 1010 b. 1100 0110

c. 3Ah d. C6h

Solutions

a. b.

0 0 1 1 1 0 1 0 ² d 1 1 0 0 0 1 1 0 ² d

1 1 0 0 0 1 0 1 ² invert bit values 0 0 1 1 1 0 0 1 ² invert bit values

+ 1 ² add 1 + 1 ² add 1

1 1 0 0 0 1 1 0 ² 2’s complement 0 0 1 1 1 0 1 0 ² 2’s complement

Page 11: Chapter 3 DATA REPRESENTATION - Computer Science …cs.wpunj.edu/~ndjatou/CS2800-chapter3.pdf · Chapter 3 DATA REPRESENTATION Binary codes are used to represent both characters and

63

© 2015 Gilbert Ndjatou

c. d.

F F F F

- 3 A ² d - C 6 ² d

C 5 ² 1's complement 3 9 ² 1's complement

+ 1 ² add 1 + 1 ² add 1

C 6 ² 2's complement 3 A ² 2's complement

Notice that the two’s complement of C6h is 3Ah, and that the two’s complement of 3Ah is C6h.

Exercise 3.4

1. Compute the two’s complement of the following 8-bit integers:

a. 01011011 b. 10110101 c. 10010110

2. Compute the two’s complement of the following 16-bit (hexadecimal) integers:

a. 1A35h b. E50Ch c. FF0A

Conversion from Decimal to Two’s Complement

It is important not to confuse the process of taking the two’s complement of

a binary or an hexadecimal integer with the two’s complement binary number

system: a two’s complement binary integer is a number in the two’s

complement binary number system. However, when you take the two’s

complement of a binary integer, you are performing an arithmetic operation.

The following rule to convert a decimal integer to two’s complement binary

system is based on the description of the two’s complement binary number

system provided above.

Page 12: Chapter 3 DATA REPRESENTATION - Computer Science …cs.wpunj.edu/~ndjatou/CS2800-chapter3.pdf · Chapter 3 DATA REPRESENTATION Binary codes are used to represent both characters and

64

© 2015 Gilbert Ndjatou

Rule 3.2 To convert a decimal integer to n-bit two’s complement

binary number system, do the following:

a. If the number is positive, convert it to base 2 (or

hexadecimal) using n bits.

b. If the number is negative:

3. Convert its magnitude (absolute value) to base 2 (or

hexadecimal) using n bits.

4. Take its two’s complement.

This rule is illustrated in Example 3.3.

Example 3.3 Conversion from decimal to two’s complement binary integers

Convert the following decimal integers to 8-bit two’s complement binary integer :

a. 10 b. 47 c. 127 d. -10 e. -47 f. -127.

Solutions

a. 10 = 00001010 = 0Ah

b. 47 = 00101111 = 2Fh

c. 127 = 01111111 = 7Fh

d. 10 = 0A the two’s complement of 0A = FF - 0A + 1 = F6

Therefore, -10 = F6h

e. 47 = 2F the two’s complement of 2F = FF - 2F + 1 = D1

Therefore, -47 = D1h

f. 127 = 7F Two’s complement of 7F = FF - 7F + 1 = 81

Therefore, -127 = 81h

Page 13: Chapter 3 DATA REPRESENTATION - Computer Science …cs.wpunj.edu/~ndjatou/CS2800-chapter3.pdf · Chapter 3 DATA REPRESENTATION Binary codes are used to represent both characters and

65

© 2015 Gilbert Ndjatou

Exercise 3.5

Convert the following decimal integers to 16-bit two’s complement binary integer, but specify your

answers in hexadecimal.

a. 27 b. -35 c. 129 d. -87 e. - 357

Sign Bit of a Two’s Complement Binary Integer

The sign bit of a two's complement binary integer is its left-most bit. It is 0

for a positive integer and 1 for a negative integer. Therefore, since the

hexadecimal digits 8, 9, A, B, C, D, E, and F all have their left-most bit 1 in

binary, and 0, 1, 2, 3, 4, 5, 6, and 7 all have their left-most bit 0, a two’s

complement binary integer represented in hexadecimal is negative if its left-

most digit is 8, 9, A, B, C, D, E or F; and positive if its left-most digit is 0,

1, 2, 3, 4, 5, 6, or 7.

Conversion from Two's Complement to Decimal

We have seen above that if you take the two’s complement twice, you obtain

the original number. For example, if you take the two’s complement of D1h

(which represents the decimal integer -47), you get back 2Fh (which

represents the decimal integer 47). We therefore have the following rule to

convert from two’s complement binary number system to decimal.

Rule 3.3 To convert a two’s complement binary integer to decimal, do

the following:

a. If it is positive (its left-most bit is 0) convert it to decimal

as if it were an unsigned integer in base 2 (or base 16).

Page 14: Chapter 3 DATA REPRESENTATION - Computer Science …cs.wpunj.edu/~ndjatou/CS2800-chapter3.pdf · Chapter 3 DATA REPRESENTATION Binary codes are used to represent both characters and

66

© 2015 Gilbert Ndjatou

b. If it is negative (its left-most bit is 1), do the following:

i) take its two’s complement

ii) convert the result in i) above to decimal as if it were

an unsigned integer in base 2 (or base 16).

iii) add the minus sign to the result in ii) above.

This rule is illustrated in Example 3.4.

Example 3.4 Conversion from Two’s Complement Binary Integers to Decimal

Convert the following 16-bit two's complement binary integers to decimal:

a. 6A0Fh b. F0F1h c. 8A00h d. 501Eh

Solutions

a. 6A0Fh is positive: convert it to decimal as if it is a binary/hexadecimal integer:

therefore, 6A0Fh = 24,576 + 2560 + 15 = 27,151 (in decimal)

b. F0F1h is negative:

i) take its two’s complement: FFFF - F0F1 + 1 = 0F0F

ii) convert to decimal: 0F0Fh = 3840 + 15 = 3855

iii) append the minus sign: F0F1h = -3855 (in decimal)

c. 8A00h is negative:

i) take its two's complement: FFFF - 8A00 + 1 = 7600

ii) convert it to decimal: 7600h = 28,672 + 1536 = 30,208

iii) append the minus sign: 8A00h = -30,208 (in decimal)

d. 501Eh is positive: convert it to decimal as if it is a binary/hexadecimal integer:

therefore, 501Eh = 20,480 + 16 + 14 = 20,510 (in decimal)

Page 15: Chapter 3 DATA REPRESENTATION - Computer Science …cs.wpunj.edu/~ndjatou/CS2800-chapter3.pdf · Chapter 3 DATA REPRESENTATION Binary codes are used to represent both characters and

67

© 2015 Gilbert Ndjatou

Exercise 3.6

Convert the following 8 and 16-bit two's complement binary integers to decimal:

a. 5A b. F5 c. 102A d. E52D

Addition of Two's Complement Binary Integers

You add n-bit two's complement binary integers the same way you add

unsigned integers in base 2 (or hexadecimal), without any concern about the

signs or the relative size of the magnitudes. However, the result of the

addition must also be an n-bit integer: any carry out of the left-most bit (or

left-most hexadecimal digit) is lost without affecting the result of the

operation. Example 3.5 illustrates the addition of integers represented in both

the decimal and 16-bit two’s complement binary number system. In Example

3.5 c. and 3.5 f., notice that the additive inverse of a two’s complement binary

integer is its two’s complement: by taking the two’s complement of a two’s

complement binary integer, and then adding the result to that integer, you

obtain the representation of zero.

Example 3.5 Addition of Two’s Complement Binary Integers

Decimal Two’s complement

a. 26 0 0 1 A

+ 6 + 0 0 0 6

32 0 0 2 0

b. 5 0 0 0 5

+ -1 + F F F F

4 â 0 0 0 4

c. 5 0 0 0 5

+ -5 + F F F B

0 â 0 0 0 0

Decimal Binary Integer

d. 2 0 0 0 2

+ -3 + F F F D

-1 F F F F

Page 16: Chapter 3 DATA REPRESENTATION - Computer Science …cs.wpunj.edu/~ndjatou/CS2800-chapter3.pdf · Chapter 3 DATA REPRESENTATION Binary codes are used to represent both characters and

68

© 2015 Gilbert Ndjatou

e. -5 F F F B

+ -1 + F F F F

-6 â F F F A

f. -20 F F E C

+ 20 + 0 0 1 4

0 â 0 0 0 0

Exercise 3.7

Perform the following operations on 8 and 16-bit two’s complement integers:

a. 1 C b. B 9 c. F F 4 C d. 1 B 2 A e. 7 A 3 D

+ B 3 + F 3 + 0 1 5 4 + 3 6 E 5 + 1 5 B 2

Subtracting Two’s Complement Binary Integers

To subtract a two's complement binary integer, first take its two’s

complement, and then add the result of this operation. Example 3.6

illustrates the subtraction of integers represented in decimal and in 16-bit

two’s complement binary number system.

Page 17: Chapter 3 DATA REPRESENTATION - Computer Science …cs.wpunj.edu/~ndjatou/CS2800-chapter3.pdf · Chapter 3 DATA REPRESENTATION Binary codes are used to represent both characters and

69

© 2015 Gilbert Ndjatou

Example 3.6 Subtracting Two’s Complement Binary Integers

Decimal Two’s complement

a. 20 0 0 1 4 0 0 1 4

- 6 - 0 0 0 6 ÷ + F F F A

14 â 0 0 0 E

b. 5 0 0 0 5 0 0 0 5

- -1 - F F F F ÷ + 0 0 0 1

6 0 0 0 6

c. -5 F F F B F F F B

- -5 - F F F B ÷ + 0 0 0 5

0 â 0 0 0 0

Exercise 3.8

Perform the following operations on 8 and 16-bit two’s complement integers:

a. A 5 b. 8 A

- E F ÷ - 0 B ÷

c. 8 6 7 D d. 3 7 A F

- E 8 F B ÷ - 2 5 8 A ÷

Page 18: Chapter 3 DATA REPRESENTATION - Computer Science …cs.wpunj.edu/~ndjatou/CS2800-chapter3.pdf · Chapter 3 DATA REPRESENTATION Binary codes are used to represent both characters and

70

© 2015 Gilbert Ndjatou

Overflow in a Two’s Complement Number System

The addition/subtraction of n-bit two’s complement binary integers generates

an overflow when the result can not be represented using n bits. As you may

notice in Example 3.5 b., 3.5 c., 3.5 e., and 3.5 f., an overflow does not

necessary occur when there is a carry out of the left-most bit (or hexadecimal

digit).

As with the sign-magnitude notation, An overflow may not occur when

you add values with opposite signs: an overflow occurs only when you add

two values with the same sign, but the result of the operation has a different

sign. Example 3.7 illustrates some addition operations that generate an

overflow.

Example 3.7 Overflow Condition

The following addition operations on two’s complement binary integers generate an overflow.

a. 0 0 0 1 (pos.) b. 7 0 A 5 (pos.) c. 8 0 0 0 (neg.)

+ 7 F F F (pos.) + 5 A 2 0 (pos.) + F F F F (neg.)

8 0 0 0 (neg.) C A C 5 (neg.) 7 F F F (pos.)

Exercise 3.9

Indicate whether or not each operation in Exercise 3.7 and Exercise 3.8 generates an overflow.

Page 19: Chapter 3 DATA REPRESENTATION - Computer Science …cs.wpunj.edu/~ndjatou/CS2800-chapter3.pdf · Chapter 3 DATA REPRESENTATION Binary codes are used to represent both characters and

71

© 2015 Gilbert Ndjatou

Conversion between 8, 16, and 32-bit Two's

Complement Integers

An 8-bit two's complement binary integer can be represented using 16 bits,

and a 16-bit two’s complement binary integer can also be represented using

32 bits. However, some 32-bit two’s complement binary integers can not be

represented using 16 bits, and some 16-bit two’s complement binary integers

can not be represented using 8 bits.

In a positional number system such as decimal, binary or hexadecimal,

you do not change the value of an unsigned integer by adding as many zeros

as you want to its left. Similarly, you do not change the value of a two’s

complement binary integer by adding its sign bit to its left as many times as

you want. Therefore, you convert an 8-bit two’s complement binary integer

to 16 bits, by expanding its sign bit to the 8 extra bit positions to the left; and

you convert a 16-bit two’s complement binary integer to 32 bits, by

expanding its sign bit to the 16 extra bit positions to the left.

You convert a 32-bit two's complement binary integer to 16 bits by

taking its lower 16 bits; and you convert a 16-bit two’s complement binary

integer to 8 bits by taking its lower 8 bits. However, this conversion will

produce an incorrect result if the 32-bit integer can not be represented using

16 bits, or the 16-bit integer can not be represented using 8 bits. This is the

case when the higher 16 bits of the 32-bit integer, or the higher 8 bits of the

16-it integer are not its sign bit. Example 3.8 illustrates the representation of

certain integers in 8, 16, and 32-bit two’s complement number system. A

dash indicates that the integer can not be represented using 8 or 16 bits.

Invalid conversions from 16-bit to 8-bit two’s complement binary integers are

illustrated in Example 3.9.

Page 20: Chapter 3 DATA REPRESENTATION - Computer Science …cs.wpunj.edu/~ndjatou/CS2800-chapter3.pdf · Chapter 3 DATA REPRESENTATION Binary codes are used to represent both characters and

72

© 2015 Gilbert Ndjatou

Example 3.8 Representations of Integers in Decimal, 8, 16, and 32-bit two’s

Complement Number System

Decimal 8-Bit 16-Bit 32-Bit

106 6A 006A 0000006A

-110 92 FF92 FFFFFF92

-1 FF FFFF FFFFFFFF

128 - 0080 00000080

-128 80 FF80 FFFFFF80

-130 - FF7E FFFFFF7E

Notice that 128 and -130 can not be represented in 8-bit two’s complement number

system.

Example 3.9 Invalid Conversions from 16-bit to 8-bit Two’s Complement Integers

16-Bit 8-Bit Comments

5A6B 6B the sign bit of 6B corresponds to 00 � 5A

F0F5 F5 the sign bit of F5 corresponds to FF � F0

0085 85 the sign bit of 85 corresponds to FF � 00

FF61 61 the sign bit of 61 corresponds to 00 � FF

Page 21: Chapter 3 DATA REPRESENTATION - Computer Science …cs.wpunj.edu/~ndjatou/CS2800-chapter3.pdf · Chapter 3 DATA REPRESENTATION Binary codes are used to represent both characters and

73

© 2015 Gilbert Ndjatou

Exercise 3.10

1. Convert the following 8-bit two’s complement binary integers to 16 bits:

a. F8 b. 6F c. 82 d. 39 e. AD

2. Convert the following 16-bit two’s complement binary integers to 8 bits, and also indicate

whether the conversion is correct or not:

a. FF87 b. 006F c. FF7B d. 0091 e. 7A52

Page 22: Chapter 3 DATA REPRESENTATION - Computer Science …cs.wpunj.edu/~ndjatou/CS2800-chapter3.pdf · Chapter 3 DATA REPRESENTATION Binary codes are used to represent both characters and

74

© 2015 Gilbert Ndjatou

Chapter 3: Exercises

1. Provide the ASCII code representation of each of the following strings of characters:

a. Math-160 b. CS_345 c. John Doe

2. Convert the following decimal integers to 16-bit two’s complement number system:

a. 49 d. -32 g. 304 j. 4057

b. -15 e. -19 h. 310 k. -5126

c. 65 f. -204 i. - 405

3. Convert the following two’s complement binary integers to decimal:

a. 3A50 c. 7F0A e. 49A2 g. 7E

b. FFA9 d. F6 f. 85 h. 9F

4. Perform the following operations on two’s complement binary integers and also indicate whether

or not there is an overflow condition:

a. FA + 5F d. FFA5 + 009A g. 84 - E5

b. 7E + 24 e. 85A6 + A5B0 h. 5A90 - 32F5

c. 3A50 + 15B6 f. 2A - 3B i. 9AF6 - 000A

j. 7FFF - 800A

5. Convert the following 8-bit two’s complement binary integers to 16 bits:

a. 74 b. F5 c. 6A d. 9B e. A0 f. 15

6. Convert the following 16-bit two’s complement binary integers to 8 bits and also indicate

whether or not the conversion is correct:

a. FF95 b. 7A2F c. 6B5C d. FF5A e. 007A f. 00A5

Page 23: Chapter 3 DATA REPRESENTATION - Computer Science …cs.wpunj.edu/~ndjatou/CS2800-chapter3.pdf · Chapter 3 DATA REPRESENTATION Binary codes are used to represent both characters and

75

© 2015 Gilbert Ndjatou

Chapter 3: Solutions of Exercises

1. a. 4D 61 74 68 2D 31 36 30 c. 4A 6F 68 6E 20 44 6F 65

b. 43 53 5F 33 34 35

2. a. 0031h d. FFE0h g. 0130h j. 0FD9h

b. FFF1 e. FFEDh h. 0136h k. EBFAh

c. 0041h f. FF34h i. FE6Bh

3. a. 14,928 c. 32,522 e. 18,850 g. 126

b. -87 d. -10 f. -123 h. -97

4. a. 59h d. 003Fh g. 9Fh j. FFF5h (ov)

b. A2h (ov) e. 2B56h (ov) h. 279Bh

c. 5006h f. EFh i. 9AECh

5. a. 0074h b. FFF5h c. 006Ah d. FF9Bh e. FFA0h f. 0015h

6. a. 95h (correct) c. 5Ch (incorrect) e. 7Ah (correct)

b. 2Fh (incorrect) d. 5Ah (incorrect) f. A5 (incorrect)