data representation

88
Data Representation Chapter 2 天天天天天天天天

Upload: zavad

Post on 06-Jan-2016

62 views

Category:

Documents


0 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Data  Representation

Data Representation

Chapter 2

天津大学软件学院

Page 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:多媒体

Page 3: Data  Representation

计算机导论

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.

Page 4: Data  Representation

计算机导论

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.

Page 5: Data  Representation

计算机导论

Analog and Digital InformationA mercury thermometer exemplifies analog data as it continually rises and falls in direct proportion to the temperature.

Digital displays only show discrete(离散的) information.

Page 6: Data  Representation

计算机导论

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.

Page 7: Data  Representation

计算机导论

Examples of bit patterns

Page 8: Data  Representation

计算机导论

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.

Page 9: Data  Representation

计算机导论

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 log2

n bit.

Number of Symbols---------------------

24816…

128256…

Bit Pattern Length---------------------

1234…

78…

Page 10: Data  Representation

计算机导论

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.

Page 11: Data  Representation

计算机导论

ASCII CODEAmerican Standard Code for Information Interchange

Page 12: Data  Representation

计算机导论

The Unicode Character Set( 统一的字符编码标准 ,采用双字节对字符进行编码)

Figure 3.6 A few characters in the Unicode character set

Page 13: Data  Representation

计算机导论

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

WAV, AU, AIFF, VQF, and MP3...

sampling: 采样quantization: 量化Coding :编码

Page 14: Data  Representation

计算机导论

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:分辨率

Page 15: Data  Representation

计算机导论

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:三基色

Page 16: Data  Representation

计算机导论

Digitized Images

Page 17: Data  Representation

计算机导论

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:数学公式

Page 18: Data  Representation

计算机导论

Representing VideoTo 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

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.

video :视频frame :祯

Page 19: Data  Representation

计算机导论

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:压缩比

Page 20: Data  Representation

计算机导论

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:有损

Page 21: Data  Representation

计算机导论

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

Page 22: Data  Representation

计算机导论

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.

SUMMARY (continued)

Page 23: Data  Representation

计算机导论

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

Page 24: Data  Representation

NumberRepresentation

Chapter 3

天津大学软件学院

Page 25: Data  Representation

计算机导论

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:十六进制

Page 26: Data  Representation

计算机导论

3.1 DECIMAL AND BINARY

Two numbering systems are dominant today in the world of computers: decimal and binary.

DECIMAL SYSTEM

Page 27: Data  Representation

计算机导论

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

binary system, 0 and 1.

Page 28: Data  Representation

计算机导论

OCTAL NOTATION

Octal notation is based on 8. This means there are 8 symbols: 0,1,2,3,4,5,6,7.

Bit PatternBit Pattern------------

000001010011

Oct DigitOct Digit------------

0123

Bit PatternBit Pattern------------

100101110111

Oct DigitOct Digit------------

4567

Page 29: Data  Representation

计算机导论

Binary to octal and octal to binary transformation

Page 30: Data  Representation

计算机导论

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 PatternBit Pattern------------

00000001001000110100010101100111

Hex DigitHex Digit------------

01234567

Bit PatternBit Pattern------------

10001001101010111100110111101111

Hex DigitHex Digit------------

89ABCDEF

Page 31: Data  Representation

计算机导论

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

Page 32: Data  Representation

计算机导论

Show the hexadecimal equivalent of the bit pattern 1100 1110 0010.

Each group of 4 bits is translated to one hexadecimal digit. The equivalent is xCE2.

SolutionSolution

Example 1Example 1

Page 33: Data  Representation

计算机导论

Show the hexadecimal equivalent of the bit pattern 0011100010B.

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 000011100010, which is translated to 0E2H.

SolutionSolution

Example 2Example 2

Page 34: Data  Representation

计算机导论

What is the bit pattern for x24C?

Write each hexadecimal digit as its equivalent bit pattern to get 001001001100.

SolutionSolution

Example 3Example 3

Page 35: Data  Representation

计算机导论

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.

Page 36: Data  Representation

计算机导论

Convert the binary number 10011 to decimal.

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 1Weights 16 8 4 2 1

------------------------------------- 16 + 0 + 0 + 2 + 1 Decimal 19

SolutionSolution

Example 1Example 1

Page 37: Data  Representation

计算机导论

DECIMAL TO BINARY CONVERSION To convert from decimal to binary, use repetitive division.

division :除法quotient: 商remainder: 余数

Page 38: Data  Representation

计算机导论

Convert the decimal number 35 to binary.

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

SolutionSolution

Example 2Example 2

Page 39: Data  Representation

计算机导论

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: 带符号整数

Page 40: Data  Representation

计算机导论

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 Number of Bits -------------------

816

RangeRange-------------------------------------0 . . . 2550 . . . 65,535

Page 41: Data  Representation

计算机导论

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.

Store 7 in an 8-bit memory location (存储单元) .

First change the number to binary 111. Add five 0s to First change the number to binary 111. Add five 0s to make a total of N (8) bits, make a total of N (8) bits, 0000011100000111. The number is . The number is stored in the memory location.stored in the memory location.

SolutionSolution

Example 3Example 3

Page 42: Data  Representation

计算机导论

Store 258 in a 16-bit memory location.

First change the number to binary 100000010. Add First change the number to binary 100000010. Add seven 0s to make a total of N (16) bits, seven 0s to make a total of N (16) bits, 00000001000000100000000100000010. The number is stored in the . The number is stored in the memory location.memory location.

SolutionSolution

Example 4Example 4

Page 43: Data  Representation

计算机导论

Overflow (溢出) If you try to store an unsigned integer such as 256 in an

8-bit memory location, you get a condition called overflow.

DecimalDecimal------------ 7 234 258 24,7601,245,678

8-bit allocation------------0000011111101010overflowoverflowoverflow

16-bit allocation16-bit allocation------------------------------

0000000000000111000000001110101000000001000000100110000010111000

overflow

Page 44: Data  Representation

计算机导论

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.

Interpret 00101011 in decimal if the number was stored as an unsigned integer.

Using the procedure shown in Figure 3.3 , the number Using the procedure shown in Figure 3.3 , the number in decimal is 43. in decimal is 43.

SolutionSolution

Example 5Example 5

Page 45: Data  Representation

计算机导论

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: 负数

Page 46: Data  Representation

计算机导论

SIGN-AND-MAGNITUDE FORMAT

Range: -(2N-1-1) —— +(2N-1-1)

There are two 0s in sign-and-magnitude representation: positive and negative. In an 8-bit allocation:+0 00000000 -0 10000000

Number of Bits Number of Bits -------------------

81632

127 032767 0 0

+0 +127 +0 +32767 +0 +2,147,483,647

RangeRange----------------------------------------------------------------------------------------------------------------------------

Page 47: Data  Representation

计算机导论

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

Page 48: Data  Representation

计算机导论

Store +7 in an 8-bit memory location using sign-and-magnitude representation.

First change the number to binary 111. Add First change the number to binary 111. Add four 0s to make a total of N-1 (7) bits, 0000111. four 0s to make a total of N-1 (7) bits, 0000111. Add an extra zero because the number is Add an extra zero because the number is positive. The result is: positive. The result is: 0000001110000111

SolutionSolution

Example 6Example 6

Page 49: Data  Representation

计算机导论

Store –258 in a 16-bit memory location using sign-and-magnitude representation.

First change the number to binary 100000010. First change the number to binary 100000010. Add six 0s to make a total of N-1 (15) bits, Add six 0s to make a total of N-1 (15) bits, 000000100000010. Add an extra 1 because the 000000100000010. Add an extra 1 because the number is negative. The result is: number is negative. The result is: 11000000100000010000000100000010

SolutionSolution

Example 7Example 7

Page 50: Data  Representation

计算机导论

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.

DecimalDecimal------------

+7-124+258

-24,760

8-bit allocation8-bit allocation------------0000011111111100overflowoverflow

16-bit allocation16-bit allocation------------------------------

0000000000000111100000000111110000000001000000101110000010111000

Page 51: Data  Representation

计算机导论

Interpret 10111011 in decimal if the number was stored as a sign-and-magnitude integer.

Ignoring the leftmost bit, the remaining bits are Ignoring the leftmost bit, the remaining bits are 0111011. This number in decimal is 59. The 0111011. This number in decimal is 59. The leftmost bit is 1, so the number is leftmost bit is 1, so the number is –59–59..

SolutionSolution

Example 8Example 8

Page 52: Data  Representation

计算机导论

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

Page 53: Data  Representation

计算机导论

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

Number of Bits Number of Bits -------------------

81632

127 -032767 -0 -0

+0 +127+0 +32767+0 +2,147,483,647

RangeRange--------------------------------------------------------------------------------------------------------------------------------------

Range: -(2N-1-1) ... +(2N-1-1)

Page 54: Data  Representation

计算机导论

Store +7 in an 8-bit memory location using one’s complement representation.

First change the number to binary 111. Add five First change the number to binary 111. Add five 0s to make a total of N (8) bits, 0s to make a total of N (8) bits, 0000011100000111. The . The sign is positive, so no more action is needed. The sign is positive, so no more action is needed. The result is: result is: 0000001110000111

SolutionSolution

Example 9Example 9

Page 55: Data  Representation

计算机导论

Store –258 in a 16-bit memory location using one’s complement representation.

First change the number to binary 100000010. First change the number to binary 100000010. Add seven 0s to make a total of N (16) bits, Add seven 0s to make a total of N (16) bits, 00000001000000100000000100000010. The sign is negative, so . The sign is negative, so each bit is complemented. The result is: each bit is complemented. The result is: 11111111011111101111111011111101

SolutionSolution

Example 10Example 10

Page 56: Data  Representation

计算机导论

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.

DecimalDecimal------------

+7-7

+124-124

+24,760-24,760

8-bit allocation8-bit allocation------------00000111111110000111110010000011overflowoverflow

16-bit allocation16-bit allocation------------------------------

000000000000011111111111111110000000000001111100111111111000001101100000101110001001111101000111

Page 57: Data  Representation

计算机导论

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.

Page 58: Data  Representation

计算机导论

Interpret 11110110 in decimal if the number was stored as a one’s complement integer.

The leftmost bit is 1, so the number is negative. First The leftmost bit is 1, so the number is negative. First complement it . The result is 00001001. The complement it . The result is 00001001. The complement in decimal is 9. So the original number complement in decimal is 9. So the original number was was –9–9. .

SolutionSolution

Example 11Example 11

Page 59: Data  Representation

计算机导论

TWO’S COMPLEMENT FORMAT(补码)

-2N-1 ... +(2N-1-1)

Number of Bits Number of Bits -------------------

81632

-128 -32,768 -2,147,483,648

0 +1270 +32,7670 +2,147,483,647

RangeRange------------------------------------------------------------------------------------------------------------

Range of two’s complement integers

Two’s complement is the most common, the most important, and the most widely used representation of integers today.

Page 60: Data  Representation

计算机导论

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.

Page 61: Data  Representation

计算机导论

Page 62: Data  Representation

计算机导论

Store +7 in an 8-bit memory location using two’s complement representation.

First change the number to binaryFirst change the number to binary 111. 111. Add five Add five 0s to make a total of N (8) bits,0s to make a total of N (8) bits, 0000011100000111.The .The sign is positive, so no more action is needed. The sign is positive, so no more action is needed. The result is: result is: 0000011100000111

SolutionSolution

Example 12Example 12

Page 63: Data  Representation

计算机导论

Store –40 in a 16-bit memory location using two’s complement representation.

First change the number to binary First change the number to binary 101000101000. Add . Add ten 0s to make a total of N (16) bits, ten 0s to make a total of N (16) bits, 00000000001010000000000000101000. The sign is negative, so . The sign is negative, so leave the rightmost 0s up to the first 1 (including leave the rightmost 0s up to the first 1 (including the 1) unchanged and complement the rest. The the 1) unchanged and complement the rest. The result is: result is: 11111111110111111111110110001000

SolutionSolution

Example 13Example 13

Page 64: Data  Representation

计算机导论

Example of storing two’s complement integers in two different computers

DecimalDecimal------------

8-bit allocation8-bit allocation------------00000111111110010111110010000100overflowoverflow

16-bit allocation16-bit allocation----------------------------

000000000000011111111111111110010000000001111100111111111000010001100000101110001001111101001000

Page 65: Data  Representation

计算机导论

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.

Page 66: Data  Representation

计算机导论

Interpret 11110110B in decimal if the number was stored as a two’s complement integer.

The leftmost bit is 1. The number is negative. Leave The leftmost bit is 1. The number is negative. Leave 1010 at at the right alone and complement the rest. The result is the right alone and complement the rest. The result is 00001010B00001010B. The two’s complement number is . The two’s complement number is 1010. So the . So the original number was original number was –10–10..

SolutionSolution

Example 14Example 14

Page 67: Data  Representation

计算机导论

Summary of integer representationContents of Contents of

MemoryMemory------------

0000000100100011010001010110011110001001101010111100110111101111

UnsignedUnsigned

------------------------00112233445566778899

101011111212131314141515

Sign-and-Sign-and-MagnitudeMagnitude

------------------+0+0+1+1+2+2+3+3+4+4+5+5+6+6+7+7-0-0-1-1-2-2-3-3-4-4-5-5-6-6-7-7

One’sOne’sComplementComplement

------------------+0+0+1+1+2+2+3+3+4+4+5+5+6+6+7+7-7-7-6-6-5-5-4-4-3-3-2-2-1-1-0-0

Two’sTwo’sComplementComplement

----------------+0+0+1+1+2+2+3+3+4+4+5+5+6+6+7+7-8-8-7-7-6-6-5-5-4-4-3-3-2-2-1-1

Page 68: Data  Representation

计算机导论

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:幻数

Page 69: Data  Representation

计算机导论

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

Page 70: Data  Representation

计算机导论

Represent –25 in Excess_127 using an 8-bit allocation.

First add 127 to get 102. First add 127 to get 102. This number in binary isThis number in binary is 1100110. 1100110. Add one bit to make it 8 bits in length. The Add one bit to make it 8 bits in length. The representation isrepresentation is 0110011001100110..

SolutionSolution

Example 15Example 15

Page 71: Data  Representation

计算机导论

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.

Interpret 11111110 if the representation is Excess_127.

First change the number to decimal. It is 254. Then First change the number to decimal. It is 254. Then subtract 127 from the number. The result is decimal subtract 127 from the number. The result is decimal 127127..

SolutionSolution

Example 16Example 16

Page 72: Data  Representation

计算机导论

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 :浮点数

Page 73: Data  Representation

计算机导论

Converting the integer part (整数部分) Converting the fraction part (小数部分)

To convert a fraction to binary, use repetitive multiplication.

Page 74: Data  Representation

计算机导论

Transform the fraction 0.875 to binary

Write the fraction at the left corner. Multiply the Write the fraction at the left corner. Multiply the number continuously by 2 and extract the number continuously by 2 and extract the integer part as the binary digit. Stop when the integer part as the binary digit. Stop when the number is 0.0.number is 0.0.

0.875 1.750 1.5 1.0 0.0

0 . 1 1 1

SolutionSolution

Example 17Example 17

Page 75: Data  Representation

计算机导论

Transform the fraction 0.4 to a binary of 6 bits.

Write the fraction at the left corner. Multiply the Write the fraction at the left corner. Multiply the number continuously by 2 and extract the number continuously by 2 and extract the integer part as the binary digit. You can never integer part as the binary digit. You can never get the exact binary representation. Stop when get the exact binary representation. Stop when you have 6 bits.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

SolutionSolution

Example 18Example 18

Page 76: Data  Representation

计算机导论

• 例例 1.00234 → 1.00

236154302345.00 → 6154302345.00

Page 77: Data  Representation

计算机导论

Original NumberOriginal Number----------------------

+1010001.1101-111.000011

+0.00000111001-001110011

MoveMove----------

6 26 3

Normalized ----------------------------- +26 x 1.01000111001 -22 x 1.11000011 +2-6 x 1.11001 -2-3 x 1.110011

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.

-.

Page 78: Data  Representation

计算机导论

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 :尾数

Page 79: Data  Representation

计算机导论

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 :双精度

Page 80: Data  Representation

计算机导论

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

Page 81: Data  Representation

计算机导论

Show the representation of the normalized number + 26 x 1.01000111001

The sign isThe sign is positive positive. The Excess_127 representation of . The Excess_127 representation of the exponent is the exponent is 133133. In binary, this is . In binary, this is 1000010110000101.. The mantissa is 0100011100101000111001.You add extra 0s on the right .You add extra 0s on the right to make it 23 bits. The number in memory is stored asto make it 23 bits. The number in memory is stored as:: 0 10000101 01000111001000000000000 0 10000101 01000111001000000000000

SolutionSolution

Example 19Example 19

Page 82: Data  Representation

计算机导论

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.

Page 83: Data  Representation

计算机导论

Interpret the following 32-bit floating-point number

1 01111100 11001100000000000000000

The sign is The sign is negativenegative. The exponent is –3 (124 – . The exponent is –3 (124 – 127). The number after normalization is127). The number after normalization is -2 -2-3-3 x 1.110011 x 1.110011

-0.001110011-0.001110011

SolutionSolution

Example 20Example 20

Page 84: Data  Representation

计算机导论

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

Page 85: Data  Representation

计算机导论

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)

Page 86: Data  Representation

计算机导论

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.

SUMMARY (continued)

Page 87: Data  Representation

计算机导论

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

Page 88: Data  Representation

计算机导论