Chapter 2 Data Representation 天津大学软件学院.

Slides:



Advertisements
Similar presentations
3.1 3 Data Storage Foundations of Computer Science  Cengage Learning.
Advertisements

Data Representation COE 202 Digital Logic Design Dr. Aiman El-Maleh
The Binary Numbering Systems
©Brooks/Cole, 2003 Chapter 3 Number Representation.
Data Representation Computer Organization &
Chapter 1 Representing Data in a Computer
Data Representation COE 205
Connecting with Computer Science, 2e
Chapter 3 Number Representation
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 12 Data.
Number Representation Rizwan Rehman, CCS, DU. Convert a number from decimal to binary notation and vice versa. Understand the different representations.
Connecting with Computer Science 2 Objectives Learn why numbering systems are important to understand Refresh your knowledge of powers of numbers Learn.
Binary Representation and Computer Arithmetic
Dr. Bernard Chen Ph.D. University of Central Arkansas
Chapter 5 Data representation.
©Brooks/Cole, 2003 Chapter 2 Data Representation.
Chapter 2 Data Representation. Define data types. Visualize how data are stored inside a computer. Understand the differences between text, numbers, images,
Simple Data Type Representation and conversion of numbers
Chapter 3 Number Representation. Convert a number from decimal 、 hexadecimal,octal to binary notation and vice versa. Understand the different representations.
Computers Organization & Assembly Language
Computers and Scientific Thinking David Reed, Creighton University Data Representation 1.
Chapter 2 Data and Number Representations
NUMBER REPRESENTATION CHAPTER 3 – part 3. ONE’S COMPLEMENT REPRESENTATION CHAPTER 3 – part 3.
IT253: Computer Organization
Studies in Big Data 4 Weng-Long Chang Athanasios V. Vasilakos MolecularComputing Towards a Novel Computing Architecture for Complex Problem Solving.
Data Representation.
Lec 3: Data Representation Computer Organization & Assembly Language Programming.
Chapter 3 Representation. Key Concepts Digital vs Analog How many bits? Some standard representations Compression Methods 3-2.
3-1 Data and Computers Computers are multimedia devices, dealing with a vast array of information categories. Computers store, present, and help us modify.
1 Foundations of Computer Science Chapter 2 Data Representation.
Number Systems Spring Semester 2013Programming and Data Structure1.
Chapter 03 Data Representation. 2 Chapter Goals Distinguish between analog and digital information Explain data compression and calculate compression.
Lecture 5. Topics Sec 1.4 Representing Information as Bit Patterns Representing Text Representing Text Representing Numeric Values Representing Numeric.
©Brooks/Cole, 2003 Chapter 2 Data Representation.
7/11/20081 Today’s Agenda Friday 6 th Nov Revision of certain topics Floating point notation Excess Notation Exam format Interim Report.
©Brooks/Cole, 2003 Chapter 3 Number Representation.
Chapter 3 Number Representation. Convert a number from decimal to binary notation and vice versa. Understand the different representations of an integer.
Number Representation
Chapter 1 Data Storage © 2007 Pearson Addison-Wesley. All rights reserved.
©Brooks/Cole, 2003 Chapter 3 Number Representation.
CISC1100: Binary Numbers Fall 2014, Dr. Zhang 1. Numeral System 2  A way for expressing numbers, using symbols in a consistent manner.  " 11 " can be.
1 Representation of Data within the Computer Oct., 1999(Revised 2001 Oct)
Chapter 2 Data Representation.
EEL 3801C EEL 3801 Part I Computing Basics. EEL 3801C Data Representation Digital computers are binary in nature. They operate only on 0’s and 1’s. Everything.
©Brooks/Cole, 2003 Chapter 2 Data Representation.
Data Representation, Number Systems and Base Conversions
DATA REPRESENTATION CHAPTER DATA TYPES Different types of data (Fig. 2.1) The computer industry uses the term “MULTIMEDIA” to define information.
Digital Fundamentals Tenth Edition Floyd Chapter 2 © 2008 Pearson Education.
Data Representation. What is data? Data is information that has been translated into a form that is more convenient to process As information take different.
©Brooks/Cole, 2003 Chapter 3 Number Representation.
Chapter 3: Data Representation Chapter 3 Data Representation Page 17 Computers use bits to represent all types of data, including text, numerical values,
3.1 3 Data Storage Foundations of Computer Science  Cengage Learning.
Data Representation COE 308 Computer Architecture
Programming and Data Structure
Data Representation ICS 233
Lec 3: Data Representation
Data Representation.
Number Representation
3 Data Storage Foundations of Computer Science ã Cengage Learning.
Chapter 3 Data Storage.
Data Structures Mohammed Thajeel To the second year students
Data Representation COE 301 Computer Organization
Ch2: Data Representation
Chapter 2 Data Representation.
Number Representation
Chapter 3 DataStorage Foundations of Computer Science ã Cengage Learning.
Data Representation Chapter 2 Computer HW (Von Neumann Model) Program
Data Representation ICS 233
OBJECTIVES After reading this chapter, the reader should be able to :
Data Representation COE 308 Computer Architecture
Presentation transcript:

Chapter 2 Data Representation 天津大学软件学院

2.1 DATA TYPES(数据类型) Data today come in different forms such as numbers, text, images, audio, and video. People need to process all these data types. The computer industry uses the term “multimedia” to define information that contains numbers, text, images, audio, and video. number:数值 text:文本 image:图像 audio:音频 Video:视频 Multimedia:多媒体

Analog and Digital (模拟和数字) Information Computers are finite(有限的). Computer memory and other hardware(硬件) devices have only so much room to store and manipulate a certain amount of data. The goal of data representation(数据表示) is to represent enough of the world to satisfy our computational needs and our senses of sight and sound.

Analog and Digital Information Information can be represented in one of two ways: analog or digital. Analog data A continuous representation, analogous to the actual information it represents. Digital data A discrete representation, breaking the information up into separate elements.

Analog and Digital Information A mercury thermometer exemplifies analog data as it continually rises and falls in direct proportion to the temperature. Digital displays only show discrete(离散的) information.

2.2 DATA INSIDE THE COMPUTER All data types from outside a computer are transformed into a uniform representation when stored in a computer and then transformed back when leaving the computer. This universal format is called a bit pattern(位组合格式). BIT(位) A bit (binary digit) is the smallest unit of data that can be stored in a computer; it is either 0 or 1. BIT PATTERN(位组合格式) A bit pattern is a sequence, or as it is sometimes called, a string of bits that can represent a symbol. e.g. BYTE(字节) A bit pattern of length 8 is called a byte.

Examples of bit patterns

2.3 REPRESENTING DATA TEXT(文本) A piece of text in any language is a sequence of symbols used to represent an idea in that language. You can represent each symbol with a bit pattern. In other words, text such as “BYTE”, which is made of four symbols, can be represented as 4 bit patterns, each pattern defining a single symbol.

Number of Symbols --------------------- How many bits are needed in a bit pattern to represent a symbol in a language? The length of the bit pattern that represents a symbol in a language depends on the number of symbols used in that language. More symbols mean a longer bit pattern. The relationship is not linear; it is logarithmic. If you need n symbols, the length is log2n bit. Number of Symbols --------------------- 2 4 8 16 … 128 256 Bit Pattern Length --------------------- 1 3 7

Codes(编码) Different sets of bit patterns have been designed to represent text symbols. Each set is called a code, and the process of representing symbols is called coding. ASCII The American National Standards Institute (ANSI) developed a code called American Standard Code for Information Interchange (ASCII)(美国信息交换标准代码 ). This code uses 7 bits for each symbol. This means 128 different symbols can be defined by this code. e.g.

ASCII CODE American Standard Code for Information Interchange

The Unicode Character Set ( 统一的字符编码标准,采用双字节对字符进行编码) Figure 3.6 A few characters in the Unicode character set

AUDIO(音频) Audio is converted to digital data, then we can use bit patterns to store them. Audio is by nature analog data. It is continuous (analog), not discrete (digital). sampling:采样 quantization:量化 Coding:编码 WAV, AU, AIFF, VQF, and MP3...

IMAGES(图像) Images today are represented in a computer by one of two methods: bitmap graphic or vector graphic. Bitmap Graphic(位图) In this method, an image is divided into a matrix of pixels (picture elements), where each pixel is a small dot. The size of the pixel depends on what is called the resolution. After dividing an image into pixels, each pixel is assigned a bit pattern. The size and the value of the pattern depend on the image. pixel:像素 resolution:分辨率

BMP, GIF, JPEG, PNG, TIFF, XBM, and PCX To represent color images, each colored pixel is decom-posed into three primary colors: red, green, and blue (RGB). Then the intensity of each color is measured, and a bit pattern (usually 8 bits) is assigned to it. In other words, each pixel has three bit patterns: one to represent the intensity of the red color, one to represent the intensity of the green color, and one to represent the intensity of the blue color. BMP, GIF, JPEG, PNG, TIFF, XBM, and PCX three primary colors:三基色

Digitized Images

WMF, PICT, EPS, SVG, SWF, and TrueType fonts Vector Graphic(矢量图) The vector graphic method does not store the bit patterns. An image is decomposed into a combination of curves and lines. Each curve or line is represented by a mathematical formula. For example, a line may be described by the coordinates of its endpoints, and a circle may be described by the coordinates of its center and the length of its radius. The combination of these formulas is stored in a computer. When the image is to be displayed or printed, the size of the image is given to the system as an input. The system redesigns the image with the new size and uses the same formula to draw the image. In this case, each time an image is drawn, the formula is reevaluated. WMF, PICT, EPS, SVG, SWF, and TrueType fonts curve:曲线, mathematical formula:数学公式

Representing Video To simulate motion, movies need to record (and play back) at least 12 frames per second. However, good sound quality requires 24 frames/s. 24 frames/s = 1440 frames/minute = 46400 frames/hour video:视频 frame:祯 If each frame has a resolution of 1024 x 768* there are 786,432 pixels in a frame. If the colour of each pixel is stored as 24 bits (3 bytes) of data, one frame alone requires 2,359,296 bytes (2 MB) of memory. An hour of film then, requires 203,843,174,400 bytes (194,400 MB – more than 190 Gigabytes) of storage – just for the images.

Data Compression(数据压缩) It is important that we find ways to store and transmit data efficiently, which leads computer scientists to find ways to compress it. Data compression is a reduction in the amount of space needed to store a piece of data. Compression ratio is the size of the compressed data divided by the size of the original data. Data compression :数据压缩 Compression ratio:压缩比

Data Compression A data compression technique can be lossless, which means the data can be retrieved without any loss of the original information, lossy, which means some information may be lost in the process of compaction. As examples, consider these 3 techniques: keyword encoding (关键字编码) run-length encoding (行程长度编码) Huffman encoding (霍夫曼编码) Lossless:无损 Lossy:有损

SUMMARY Numbers, text, images, audio, and video are all forms of data. Computers need to process all types of data. All data types are transformed into a uniform representation called a bit pattern for processing by computers. A bit is the smallest unit of data that can be stored in a computer. A bit pattern is a sequence of bits that can represent a symbol. A byte is 8 bits.

SUMMARY (continued) Coding is the process of transforming data into a bit pattern. ASCII is a popular code for symbols. Images use the bitmap graphic or vector graphic method for data representation. The image is broken up into pixels which can then be assigned bit patterns. Audio data are transformed to bit patterns though sampling, quantization, and coding. Video data are a set of sequential images.

EXERCISES 2-1;2-2; 2-11;2-12;2-13;2-14;2-15 2-23;2-24;2-25;2-26;2-27 2-34;2-35;2-36;2-37;2-38;2-39

Number Representation Chapter 3 Number Representation 天津大学软件学院

• The Decimal system is based on 10,0-9; Number System • The Decimal system is based on 10,0-9; • The binary system is based on 2,0-1; • Octal notation is based on 8,0-7; • Hexadecimal notation is based on 16 ,0-9,A-F。 Decimal system:十进制 binary system:二进制 Octal notation:八进制 Hexadecimal notation:十六进制

3.1 DECIMAL AND BINARY DECIMAL SYSTEM Two numbering systems are dominant today in the world of computers: decimal and binary. DECIMAL SYSTEM

BINARY SYSTEM The binary system is based on 2. There are only two digits in the binary system, 0 and 1.

OCTAL NOTATION Octal notation is based on 8. This means there are 8 symbols: 0,1,2,3,4,5,6,7. Bit Pattern ------------ 000 001 010 011 Oct Digit ------------ 1 2 3 Bit Pattern ------------ 100 101 110 111 Oct Digit ------------ 4 5 6 7

Binary to octal and octal to binary transformation

HEXADECIMAL NOTATION Hexadecimal notation is based on 16 (hexadec is Greek for 16). This means there are 16 symbols (hexadecimal digits): 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E, and F. Each hexadecimal digit can represent 4 bits, 4 bits can be represented by a hexadecimal digit. Bit Pattern ------------ 0000 0001 0010 0011 0100 0101 0110 0111 Hex Digit ------------ 1 2 3 4 5 6 7 1000 1001 1010 1011 1100 1101 1110 1111 8 9 A B C D E F

CONVERSION(转换) Converting from a bit pattern to hexadecimal is done by organizing the pattern into groups of four and finding the hexadecimal value for each group of 4 bits. For hexadecimal to bit pattern conversion, convert each hexadecimal digit to its 4-bit equivalent. Hexadecimal notation is written in two formats. In the first format, you add a lowercase (or uppercase) x before the digits. For example, xA34; In another format, you indicate the base of the number (16) as the subscript after the notation. For example, (A34)16.;A34H

Show the hexadecimal equivalent of the bit pattern 1100 1110 0010. Example 1 Show the hexadecimal equivalent of the bit pattern 1100 1110 0010. Solution Each group of 4 bits is translated to one hexadecimal digit. The equivalent is xCE2.

Show the hexadecimal equivalent of the bit pattern 0011100010B. Example 2 Show the hexadecimal equivalent of the bit pattern 0011100010B. Solution Divide the bit pattern into 4-bit groups (from the right). In this case, add two extra 0s at the left to make the number of bits divisible by 4. So you have 0000 1110 0010, which is translated to 0E2H.

What is the bit pattern for x24C? Example 3 What is the bit pattern for x24C? Solution Write each hexadecimal digit as its equivalent bit pattern to get 0010 0100 1100.

3.2 CONVERSION BINARY TO DECIMAL CONVERSION Start with the binary number and multiply each binary digit by its weight. Since each binary bit can be only 0 or 1, the result will be either 0 or the value of the weight. After multiplying all the digits, add the results.

Convert the binary number 10011 to decimal. Example 1 Convert the binary number 10011 to decimal. Solution Write out the bits and their weights. Multiply the bit by its corresponding weight and record the result. At the end, add the results to get the decimal number. Binary 1 0 0 1 1 Weights 16 8 4 2 1 ------------------------------------- 16 + 0 + 0 + 2 + 1 Decimal 19

DECIMAL TO BINARY CONVERSION To convert from decimal to binary, use repetitive division. division:除法 quotient:商 remainder:余数

Convert the decimal number 35 to binary. Example 2 Convert the decimal number 35 to binary. Solution Write out the number at the right corner. Divide the number continuously by 2 and write the quotient and the remainder. The quotients move to the left, and the remainder is recorded under each quotient. Stop when the quotient is zero. 0  1  2  4  8  17  35 Dec. Binary 1 0 0 0 1 1

3.3 INTEGER REPRESENTATION(整数表示法 ) Integers are whole numbers (i.e., numbers without a fraction). An integer can be positive or negative. −∞ ←− 0 −→ +∞ To use computer memory more efficiently, two broad categories of integer representation have been developed: unsigned integers and signed integers . Signed integers may also be represented in three distinct ways. Integer:整数 fraction:分数 unsigned integer:无符号整数 signed integer:带符号整数

Number of Bits ------------------- UNSIGNED INTEGERS FORMAT An unsigned integer is an integer without a sign. Most computers define a constant called the maximum unsigned integer. An unsigned integer ranges between 0 and this constant. The maximum unsigned integer depends on the number of bits the computer allocates to store an unsigned integer. Range: 0 ... (2N-1) N is the number of bits allocated to represent one unsigned integer. Number of Bits ------------------- 8 16 Range ------------------------------------- 0 . . . 255 0 . . . 65,535

Store 7 in an 8-bit memory location(存储单元). Representation Storing unsigned integers is a straightforward process as outlined in the following step: 1. The number is changed to binary. 2. If the number of bits is less than N, 0s are added to the left of the binary number so that there is a total of N bit. Example 3 Store 7 in an 8-bit memory location(存储单元). Solution First change the number to binary 111. Add five 0s to make a total of N (8) bits, 00000111. The number is stored in the memory location.

Example 4 Store 258 in a 16-bit memory location. Solution First change the number to binary 100000010. Add seven 0s to make a total of N (16) bits, 0000000100000010. The number is stored in the memory location.

------------------------------ Decimal ------------ 7 234 258 24,760 1,245,678 8-bit allocation ------------ 00000111 11101010 overflow 16-bit allocation ------------------------------ 0000000000000111 0000000011101010 0000000100000010 0110000010111000 Overflow(溢出) If you try to store an unsigned integer such as 256 in an 8-bit memory location, you get a condition called overflow.

Using the procedure shown in Figure 3.3 , the number in decimal is 43. Interpretation How do you interpret an unsigned binary representation in decimal? The process is simple. Change the N bits from the binary system to the decimal system. Example 5 Interpret 00101011 in decimal if the number was stored as an unsigned integer. Solution Using the procedure shown in Figure 3.3 , the number in decimal is 43.

SIGNED INTEGERS FORMAT SIGN-AND-MAGNITUDE FORMAT(原码) In sign-and-magnitude representation the leftmost bit defines the sign of the number. If it is 0, the number is positive. If it is 1, the number is negative positive:正数 negative:负数

Range: -(2N-1-1) —— +(2N-1-1) SIGN-AND-MAGNITUDE FORMAT Range: -(2N-1-1) —— +(2N-1-1) Number of Bits ------------------- 8 16 32 -127 -0 -32767 -0 -2,147,483,647 -0 +0 +127 +0 +32767 +0 +2,147,483,647 Range -------------------------------------------------------------- There are two 0s in sign-and-magnitude representation: positive and negative. In an 8-bit allocation: +0  00000000 -0  10000000

Representation Storing sign-and-magnitude integer is a straightforward process: 1. The number is changed to binary; the sign is ignored. 2. If the number of bits is less than N-1, 0s are added to the left of the number so that there is a total of N-1 bits. 3. If the number is positive, 0 is added to the left (to make it N bits). If the number is negative, 1 is added to the left (to make it N bits).

Example 6 Store +7 in an 8-bit memory location using sign-and-magnitude representation. Solution First change the number to binary 111. Add four 0s to make a total of N-1 (7) bits, 0000111. Add an extra zero because the number is positive. The result is: 00000111

Example 7 Store –258 in a 16-bit memory location using sign-and-magnitude representation. Solution First change the number to binary 100000010. Add six 0s to make a total of N-1 (15) bits, 000000100000010. Add an extra 1 because the number is negative. The result is: 1000000100000010

------------------------------ Decimal ------------ +7 -124 +258 -24,760 8-bit allocation ------------ 00000111 11111100 overflow 16-bit allocation ------------------------------ 0000000000000111 1000000001111100 0000000100000010 1110000010111000 Interpretation How do you interpret a sign-and-magnitude binary representation in decimal? The process is simple: 1. Ignore the first (leftmost) bit. 2. Change the N-1 bits from binary to decimal as shown at the beginning of the chapter. 3. Attach a + or a – sign to the number based on the leftmost bit.

Example 8 Interpret 10111011 in decimal if the number was stored as a sign-and-magnitude integer. Solution Ignoring the leftmost bit, the remaining bits are 0111011. This number in decimal is 59. The leftmost bit is 1, so the number is –59.

ONE’S COMPLEMENT FORMAT(反码) One’s complement of a number is obtained by changing all 0s to 1s and all 1s to 0s. The leftmost bit defines the sign of the number. If it is 0, the number is positive. If it is 1, the number is negative. A positive number is presented by it’s SIGN-AND- MAGNITUDE FORMAT A negative number is presented by it’s ONE’S COMPLEMENT FORMAT There are two 0s in one’s complement representation: positive and negative. In an 8-bit allocation: +0  00000000 -0  11111111

Storing one’s complement integers requires the following steps: Range: -(2N-1-1) ... +(2N-1-1) Number of Bits ------------------- 8 16 32 Range ------------------------------------------------------------------- -127 -0 -32767 -0 -2,147,483,647 -0 +0 +127 +0 +32767 +0 +2,147,483,647 Representation Storing one’s complement integers requires the following steps: 1. The number is changed to binary; the sign is ignored. 2. 0s are added to the left of the number to make a total of N bits. 3. If the sign is positive, no more action is needed. If the sign is negative, every bit is complemented (changed from 0 to 1 or from 1 to 0).

Example 9 Store +7 in an 8-bit memory location using one’s complement representation. Solution First change the number to binary 111. Add five 0s to make a total of N (8) bits, 00000111. The sign is positive, so no more action is needed. The result is: 00000111

Example 10 Store –258 in a 16-bit memory location using one’s complement representation. Solution First change the number to binary 100000010. Add seven 0s to make a total of N (16) bits, 0000000100000010. The sign is negative, so each bit is complemented. The result is: 1111111011111101

------------------------------ Decimal ------------ +7 -7 +124 -124 +24,760 -24,760 8-bit allocation ------------ 00000111 11111000 01111100 10000011 overflow 16-bit allocation ------------------------------ 0000000000000111 1111111111111000 0000000001111100 1111111110000011 0110000010111000 1001111101000111 One’s complement means reversing all bits. If you one’s complement a positive number, you get the corresponding negative number. If you one’s complement a negative number, you get the corresponding positive number. If you one’s complement a number twice, you get the original number.

Interpretation How do you interpret a one’s complement binary repre-sentation in decimal? The process involves these steps: 1. If the leftmost bit is 0 (positive number), a. Change the entire number from binary to decimal. b. Put a plus sign (+) in front of the number. 2. If the leftmost bit is 1 (negative number), a. Complement the entire number (changing all 0s to 1s, and vice versa). b. Change the entire number from binary to decimal. c. Put a negative sign (-) in front of the number.

Example 11 Interpret 11110110 in decimal if the number was stored as a one’s complement integer. Solution The leftmost bit is 1, so the number is negative. First complement it . The result is 00001001. The complement in decimal is 9. So the original number was –9.

------------------------------------------------------ TWO’S COMPLEMENT FORMAT(补码) Two’s complement is the most common, the most important, and the most widely used representation of integers today. Range of two’s complement integers -2N-1 ... +(2N-1-1) Number of Bits ------------------- 8 16 32 -128 -32,768 -2,147,483,648 0 +127 0 +32,767 0 +2,147,483,647 Range ------------------------------------------------------

Representation Storing two’s complement requires the following steps: 1. The number is changed to binary; the sign is ignored. 2. If the number of bits is less than N, 0s are added to the left of number so that there is a total of N bits. 3. If the sign is positive, no further action is needed. If the sign is negative, leave all the rightmost 0s and the first 1 unchanged. Complement the rest of the bits.

Example 12 Store +7 in an 8-bit memory location using two’s complement representation. Solution First change the number to binary 111. Add five 0s to make a total of N (8) bits, 00000111.The sign is positive, so no more action is needed. The result is: 00000111

Example 13 Store –40 in a 16-bit memory location using two’s complement representation. Solution First change the number to binary 101000. Add ten 0s to make a total of N (16) bits, 0000000000101000. The sign is negative, so leave the rightmost 0s up to the first 1 (including the 1) unchanged and complement the rest. The result is: 1111111111011000

---------------------------- Example of storing two’s complement integers in two different computers Decimal ------------ +7 -7 +124 -124 +24,760 -24,760 8-bit allocation ------------ 00000111 11111001 01111100 10000100 overflow 16-bit allocation ---------------------------- 0000000000000111 1111111111111001 0000000001111100 1111111110000100 0110000010111000 1001111101001000

Interpretation How do you interpret a two's complement binary represen-tation in decimal? The process involves these steps: 1. If the leftmost bit is 0 (positive number), a. Change the whole number from binary to decimal. b. Put a plus sign (+) in front of the number . 2. If the leftmost bit is 1 (negative number), a. Leave the rightmost bits up to the first 1 (inclusive) unchanged. Complement the rest of the bits. b. Change the whole number from binary to decimal. c. Put a negative sign (-) in front of the number.

Example 14 Interpret 11110110B in decimal if the number was stored as a two’s complement integer. Solution The leftmost bit is 1. The number is negative. Leave 10 at the right alone and complement the rest. The result is 00001010B. The two’s complement number is 10. So the original number was –10.

Contents of Memory ------------ Summary of integer representation Contents of Memory ------------ 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 Unsigned ------------ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Sign-and-Magnitude --------- +0 +1 +2 +3 +4 +5 +6 +7 -0 -1 -2 -3 -4 -5 -6 -7 One’s Complement Two’s Complement -------- -8

3.4 EXCESS SYSTEM Another representation that allows you to store both positive and negative numbers in a computer is called the Excess system. In this system, it is easy to transform a number from decimal to binary, and vice versa. However, operations on the numbers are very complicated. The only application in use today is in storing the exponential value of a fraction. This is discussed in the next section. In an excess conversion, a positive number, called the magic number, is used in the conversion process. The magic number is normally (2N-1) or (2N-1-1), where N is the bit allocation. For example, if N is 8, the magic number is either 128 or 127. In the first case, we call the representation Excess_128, and in the second case, it is Excess_127. exponential :指数的 magic number:幻数

To represent a number in Excess, use the following procedure: Representation To represent a number in Excess, use the following procedure: 1. Add the magic number to the integer. 2. Change the result to binary and add 0s so that there is a total of N bits. 4位二进制数表示带符号整数 移7系统 加7

Example 15 Represent –25 in Excess_127 using an 8-bit allocation. Solution First add 127 to get 102. This number in binary is 1100110. Add one bit to make it 8 bits in length. The representation is 01100110.

Interpret 11111110 if the representation is Excess_127. Interpretation To interpret a number in excess, use the following procedure: 1. Change the number to decimal. 2. Subtract the magic number from the integer. Example 16 Interpret 11111110 if the representation is Excess_127. Solution First change the number to decimal. It is 254. Then subtract 127 from the number. The result is decimal 127.

3.5 FLOATING-POINT REPRESENTATION To represent a floating-point number (a number containing an integer and a fraction), the number is divided into two parts: the integer and the fraction. For example, the floating-point number 14.234 has an integer of 14 and a fraction of 0.234. CONVERTING TO BINARY To convert a floating-point number to binary, use the following procedure: 1. Convert the integer part to binary. 2. Convert the fraction to binary. 3. Put a decimal point between the two parts. floating-point number:浮点数

Converting the integer part(整数部分) Converting the fraction part(小数部分) To convert a fraction to binary, use repetitive multiplication.

Example 17 Transform the fraction 0.875 to binary Solution Write the fraction at the left corner. Multiply the number continuously by 2 and extract the integer part as the binary digit. Stop when the number is 0.0. 0.875  1.750  1.5  1.0  0.0 0 . 1 1 1

Example 18 Transform the fraction 0.4 to a binary of 6 bits. Solution Write the fraction at the left corner. Multiply the number continuously by 2 and extract the integer part as the binary digit. You can never get the exact binary representation. Stop when you have 6 bits. 0.4  0.8  1.6  1.2  0.4  0.8  1.6 0 . 0 1 1 0 0 1

例 1.00234 → 1.00 236154302345.00 → 6154302345.00

Original Number ---------------------- NORMALIZATION(标准化,规格化) (+10011.1011), (+10.0111011),(-10011101.1) A standard representation for floating-point numbers. Normalization is the moving of the decimal point so that there is only one 1 to the left of the decimal point. 1.XXXXXXXXXXXXXXXXX To indicate the original value of the number, multiply the number by 2e, where e is the number of bits that the decimal points moved: positive for left movement, negative for right movement. A positive or negative sign is then added depending on the sign of the original number. Original Number ---------------------- +1010001.1101 -111.000011 +0.00000111001 -001110011 Move ----------  6  2 6  3  Normalized ----------------------------- +26 x 1.01000111001 -22 x 1.11000011 +2-6 x 1.11001 -2-3 x 1.110011 -.

SIGN, EXPONENT, AND MANTISSA After a number is normalized, you store only three pieces of information about the number: sign, exponent, and mantissa (the bits to the right of the decimal point). For example, +1000111.0101 becomes + 26 × x1.0001110101 Sign: + Exponent: 6 Mantissa: 0001110101 Sign The sign of the number can be stored using 1 bit (0 or 1). Exponent The exponent (power of 2) defines the movement of the decimal point. Excess representation is the method used to store the exponent. Mantissa It defines the precision of the number. The mantissa is stored as an unsigned integer. SIGN:符号, EXPONENT :指数,mantissa:尾数

IEEE STANDARDS The institute of electrical and electronics engineers (IEEE) has defined three standards for storing floating-point number; two are used to store numbers in memory: single precision and double precision. IEEE :电气和电子工程师协会 single precision :单精度 double precision:双精度

Single-Precision Representation The procedure for storing a normalized floating-point number in memory using single-precision format is as follows: 1. Store the sign as 0 (positive) or 1 (negative). 2. Store the exponent (power of 2) as excess_127(8 bits). 3. Store the mantissa as an unsigned integer(23 bits).

Example 19 Show the representation of the normalized number + 26 x 1.01000111001 Solution The sign is positive. The Excess_127 representation of the exponent is 133. In binary, this is 10000101. The mantissa is 01000111001.You add extra 0s on the right to make it 23 bits. The number in memory is stored as: 0 10000101 01000111001000000000000

Floating-point interpretation for single precision The following procedure interprets a 32-bit floating-point number stored in memory. 1. Use the leftmost bit as the sign. 2.Change the next 8 bits to decimal and subtract 127 from it. This is the exponent. 3. Add 1 and a decimal point to the next 23 bits. You can ignore any extra 0s at the right. 4. Move the decimal point to the correct position using the value of the exponent. 5. Change the whole part to decimal. 6. Change the fraction part to decimal. 7. Combine the whole and the fraction parts.

Example 20 Interpret the following 32-bit floating-point number 1 01111100 11001100000000000000000 Solution The sign is negative. The exponent is –3 (124 – 127). The number after normalization is -2-3 x 1.110011 -0.001110011

SUMMARY The decimal system has 10 digits and is based on powers of 10. The binary system, used by computers to store numbers, has 2 digits, 0 and 1, and is based on powers of 2. The bit allocation is the number of bits used to represent an integer. Integers can be represented as unsigned or signed numbers. There are three major methods of signed number representation: sign-and-magnitude, one's complement, and two's complement. Unsigned numbers are commonly used for counting and addressing.

SUMMARY (continued) In the sign-and-magnitude method of integer representation, 1 bit represents the sign of the number; the remaining bits represent the magnitude. In the one's complement method of integer representation, a negative number is represented by complementing the corresponding positive number. Complementing a number means to convert each 1 to 0 and each 0 to 1. In the two's complement method of integer representation, a negative number is represented by leaving all the rightmost 0s and the first 1 unchanged and then complementing the remaining bits.

SUMMARY (continued) Most computers today use the two's complement method of integer representation. Both sign-and-magnitude and one's complement methods have two representations for the 0 value; two's complement has just one representation for the 0 value. A floating-point number is a whole and a fraction. Conversion of the fraction to binary requires the denominator of the fraction to be expressed as a power of 2. The Excess-X system is used to store this power of 2. A fraction is normalized so that operations are simpler. To store a fraction in memory, you need its sign, exponent and mantissa.

作业 3-20——3-36 3-47——3-50,52-62 3-67——3-68,3-72,3-73