Chapter 3 DATA REPRESENTATION. 3.1 Character Representation

Similar documents
Today. Binary addition Representing negative numbers. Andrew H. Fagg: Embedded Real- Time Systems: Binary Arithmetic

Computer Science 281 Binary and Hexadecimal Review

Binary Representation. Number Systems. Base 10, Base 2, Base 16. Positional Notation. Conversion of Any Base to Decimal.

Chapter 4: Computer Codes

The string of digits in the binary number system represents the quantity

Number Representation

Solution for Homework 2

EE 261 Introduction to Logic Circuits. Module #2 Number Systems

LSN 2 Number Systems. ECT 224 Digital Computer Fundamentals. Department of Engineering Technology

Numeral Systems. The number twenty-five can be represented in many ways: Decimal system (base 10): 25 Roman numerals:

Lecture 2. Binary and Hexadecimal Numbers

Oct: 50 8 = 6 (r = 2) 6 8 = 0 (r = 6) Writing the remainders in reverse order we get: (50) 10 = (62) 8

Levent EREN A-306 Office Phone: INTRODUCTION TO DIGITAL LOGIC

Data Storage 3.1. Foundations of Computer Science Cengage Learning

2011, The McGraw-Hill Companies, Inc. Chapter 3

Data Storage. Chapter 3. Objectives. 3-1 Data Types. Data Inside the Computer. After studying this chapter, students should be able to:

Numbering Systems. InThisAppendix...

Binary Representation

Systems I: Computer Organization and Architecture

CS101 Lecture 11: Number Systems and Binary Numbers. Aaron Stevens 14 February 2011

Binary Numbering Systems

CDA 3200 Digital Systems. Instructor: Dr. Janusz Zalewski Developed by: Dr. Dahai Guo Spring 2012

To convert an arbitrary power of 2 into its English equivalent, remember the rules of exponential arithmetic:

Number and codes in digital systems

MACHINE INSTRUCTIONS AND PROGRAMS

Chapter 1: Digital Systems and Binary Numbers

HOMEWORK # 2 SOLUTIO

Digital Logic Design. Introduction

Digital System Design Prof. D Roychoudhry Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

plc numbers Encoded values; BCD and ASCII Error detection; parity, gray code and checksums

A single register, called the accumulator, stores the. operand before the operation, and stores the result. Add y # add y from memory to the acc

Useful Number Systems

This 3-digit ASCII string could also be calculated as n = (Data[2]-0x30) +10*((Data[1]-0x30)+10*(Data[0]-0x30));

ECE 0142 Computer Organization. Lecture 3 Floating Point Representations

1. Give the 16 bit signed (twos complement) representation of the following decimal numbers, and convert to hexadecimal:

Z80 Instruction Set. Z80 Assembly Language

COMBINATIONAL CIRCUITS

Intel Hexadecimal Object File Format Specification Revision A, 1/6/88

BCD (ASCII) Arithmetic. Where and Why is BCD used? Packed BCD, ASCII, Unpacked BCD. BCD Adjustment Instructions AAA. Example

Divide: Paper & Pencil. Computer Architecture ALU Design : Division and Floating Point. Divide algorithm. DIVIDE HARDWARE Version 1

2010/9/19. Binary number system. Binary numbers. Outline. Binary to decimal

The Hexadecimal Number System and Memory Addressing

Lecture 8: Binary Multiplication & Division

Lecture 11: Number Systems

Digital Design. Assoc. Prof. Dr. Berna Örs Yalçın

Binary, Hexadecimal, Octal, and BCD Numbers

Hexadecimal Object File Format Specification

Encoding Text with a Small Alphabet

DNA Data and Program Representation. Alexandre David

This Unit: Floating Point Arithmetic. CIS 371 Computer Organization and Design. Readings. Floating Point (FP) Numbers

The Answer to the 14 Most Frequently Asked Modbus Questions

Counting in base 10, 2 and 16

Computers. Hardware. The Central Processing Unit (CPU) CMPT 125: Lecture 1: Understanding the Computer

BINARY CODED DECIMAL: B.C.D.

1. Convert the following base 10 numbers into 8-bit 2 s complement notation 0, -1, -12

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

Unpacked BCD Arithmetic. BCD (ASCII) Arithmetic. Where and Why is BCD used? From the SQL Server Manual. Packed BCD, ASCII, Unpacked BCD

ASCII and BCD Arithmetic. Chapter 11 S. Dandamudi

Chapter 2. Binary Values and Number Systems

Chapter Binary, Octal, Decimal, and Hexadecimal Calculations

Appendix C: Keyboard Scan Codes

Appendix A Installation of Visual C++ and MASM

ASCII Characters. 146 CHAPTER 3 Information Representation. The sign bit is 1, so the number is negative. Converting to decimal gives

The use of binary codes to represent characters

2 ASCII TABLE (DOS) 3 ASCII TABLE (Window)

CSI 333 Lecture 1 Number Systems

Management Challenge. Managing Hardware Assets. Central Processing Unit. What is a Computer System?

Section 1.4 Place Value Systems of Numeration in Other Bases

ASCII Code. Numerous codes were invented, including Émile Baudot's code (known as Baudot

MEP Y9 Practice Book A

=

M6800. Assembly Language Programming

Binary Division. Decimal Division. Hardware for Binary Division. Simple 16-bit Divider Circuit

Positional Numbering System

Number Systems and Radix Conversion

The x86 PC: Assembly Language, Design, and Interfacing 5 th Edition

Binary Numbers. Binary Octal Hexadecimal

MICROPROCESSOR AND MICROCOMPUTER BASICS

I PUC - Computer Science. Practical s Syllabus. Contents

AN1304. NFC Type MIFARE Classic Tag Operation. Application note PUBLIC. Rev October Document information

Today s topics. Digital Computers. More on binary. Binary Digits (Bits)

Programmer s Reference

Faculty of Engineering Student Number:

CPU Organization and Assembly Language

NUMBER SYSTEMS. 1.1 Introduction

Chapter 4 Register Transfer and Microoperations. Section 4.1 Register Transfer Language

Numerical Matrix Analysis

CHAPTER 5 Round-off errors

CS201: Architecture and Assembly Language

SECTION C [short essay] [Not to exceed 120 words, Answer any SIX questions. Each question carries FOUR marks] 6 x 4=24 marks

Binary Adders: Half Adders and Full Adders

Counters. Present State Next State A B A B

Base Conversion written by Cathy Saxton

Chapter 2 Basic Structure of Computers. Jin-Fu Li Department of Electrical Engineering National Central University Jungli, Taiwan

Instruction Set Architecture (ISA)

Sistemas Digitais I LESI - 2º ano

ASSEMBLY LANGUAGE PROGRAMMING (6800) (R. Horvath, Introduction to Microprocessors, Chapter 6)

Arithmetic in MIPS. Objectives. Instruction. Integer arithmetic. After completing this lab you will:

Technical Information. Digital Signals. 1 bit. Part 1 Fundamentals

COMPSCI 210. Binary Fractions. Agenda & Reading

Transcription:

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. 53

54 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 7 are used to encode information. It then follows that only 128 (2 ) characters can be encoded. Using all 8 bits to encode information makes it possible to 8 represent 256 (2 ) characters. This extension of the ASCII code is used on 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). 7 8 With only 128 (2 ) character codes for the ASCII code and 256 (2 ) 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 16 Unicode. The 16-bit Unicode has 65,536 (2 ) character codes, making it 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?

55 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 8 in the range 0 to 511 (2-1 ) and the 16-bit unsigned integers represent 16 integer values in the range 0 to 65,535 (2-1 ). 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.

56 Sign-Magnitude Number Systems In n-bit sign-magnitude number system, a signed integer is represented as: s b n-2 b n-3... b 2 b 1 b 0 where b n-2 b n-3... b 2 b 1 b 0 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 - n-1 n-1 (2-1) to 2-1. This representation uses n-1 bits to represent the absolute value of an n-1 integer, with the largest absolute value being the decimal value 2-1. So, in 16-bit sign 15 15 magnitude, only the integer values in the range -2 +1 = -32,767 to 2-1 = 32,767 can be represented. The rules for sign-magnitude addition are the same that we use for decimal integer addition:

57 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.

58 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 n In n-bit two s complement binary number system, 2 n-bit codes are used to represent positive and negative integers as follows: n-1 n-1! the binary representation of 2 using n bits is used to represent -2! the binary representation (using n bits) of 0 or any positive integer n-1 less than 2 is used to represent that integer n-1! for each positive integer m less than 2 with n-bit code d, the n-bit n code that represents -m is 2 - d. It follows from the above description that in n-bit two s complement binary number system, we have the following conditions: n-1 a) only the positive integers less than 2 can be represented. Morever, a positive integer is represented by its binary representation using n bits. n-1 b) the smallest negative integer that can be represented is -2. It is n-1 represented by the binary representation (using n bits) of 2.

59 c) if d is the n-bit code that represents a positive integer m, then the n-bit n code that represents -m is 2 - d. 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 n-1 n-1 represented in n-bit two s complement number system is -2 to 2-1. For example, in 8-bit two s complement number system, the range of decimal 7 7 integers is -2 = - 128 to 2-1 = 127, and in 16-bit the range of decimal 15 15 integers is -2 = -32,768 to 2-1 = 32,767. 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 n The two s complement of an n-bit integer d is to 2 - d. For example, the two s complement of the 8-bit integer 00001011 is computed as follows: 8 1 0 0 0 0 0 0 0 0 2-0 0 0 0 1 0 1 1 d 1 1 1 1 0 1 0 1 Two s complement of d

60 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 15-2 = -32,768 8000 15-2 +1= -32,767 8001 15-2 +2= -32,766 8002 7-2 -1= -129 - FF7F 7-2 = -128 80 FF80 7-2 +1= -127 81 FF81-2 FE = 100-2 FFFE = 10000h - 2-1 FF = 100h - 1 FFFF = 10000h - 1 0 00 0000 1 01 0001 7 2-1= 127 7F 007F 7 2 = 128-0080 15 2-1 = 32,767. 7FFF

61 n n n Note that 2 - d = (2-1) - d + 1 and (2-1) is an n-bit integer consisting of 1's. Therefore, the two s complement of the 8-bit integer above can also be computed as follows: 8 1 1 1 1 1 1 1 1 2-1 - 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.

62 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 n n n n the original number: 2 - (2 - d) = 2-2 + d = d. However, if d is the n-1 n-bit two s complement binary code that represents -2, then the two s 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

63 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.

64 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

65 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 leftmost 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).

66 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)

67 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 c. 5 0 0 0 5 + -5 + F F F B 0 0 0 0 0 b. 5 0 0 0 5 + -1 + F F F F 4 0 0 0 4 Decimal Binary Integer d. 2 0 0 0 2 + -3 + F F F D -1 F F F F

68 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.

69 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

70 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.

Conversion between 8, 16, and 32-bit Two's Complement Integers 71 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.

72 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

73 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

74 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

75 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)