Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3 Number Representation

Similar presentations


Presentation on theme: "Chapter 3 Number Representation"— Presentation transcript:

1 Chapter 3 Number Representation
電腦的數字運算為什麼是二進位? 人類用 10 進位. ET會用8進位吧! 小叮噹會用2進位吧! 然而實際上不只有10進位, 時鐘混用了 12 進位與 60 進位, 天干地支是 12 進位. 角度 360 進位. ET 數巧克力球時, 數到第 8 個巧克力球時, 手指就不夠用了, 需要借用 ET 媽媽的一支手指頭, 而把自己的手指全部收回來. 等到 ET 數到第 64 個巧克力球時, ET 媽媽的手指就不夠用了, 需要借用 ET 爸爸的一支手指頭, ET 和 ET 媽媽的手指全部收回來. 所以 254 個巧克力球, 254=64*2+8*5+4, 需要用 ET 爸爸的 2 支手指, ET 媽媽的 5 支手指, ET 的 4 支手指.

2 Objective 在這一章中,我們將學習在電腦中使用的數字系統:在電腦中如何用一串的0與1來表示一個數字,如何將一個數字儲存起來。
我們將會學到表示整數與表示小數的各種方式。特別要注意的是,由於在電腦中是以固定長度的0/1字串來表示數字,一定會有可表示的數字的範圍。而且會有些數字會無法精確的表示出來,只能儘量提高其精確度。 After reading this chapter, the reader should be able to: Convert a number from decimal to binary notation and vice versa. Understand the different representations of an integer inside a computer: unsigned, sign-and-magnitude, one’s complement, and two’s complement. Understand the Excess system that is used to store the exponential part of a floating-point number. Understand how floating numbers are stored inside a computer using the exponent and the mantissa.

3 Outlines Decimal and Binary Conversion Integer Representation
Excess System Floating-Point Representation Hexadecimal Notation Key Terms Summary Homework

4 Section 3.1 Decimal and Binary
在第2章中我們層經討論許多不是數字的資料表示法, 雖然其中有 ASCII 0-9 的數字表示, 但是若要用 ASCII 來儲存一個數字, 如 65535, 便需要 5 bytes. 如果用 unsigned number, 就只需要 2 個 bytes. 因此用 ASCII 來表示數字, 在 size 上是不 efficiency. 此外若以 ASCII character 來表示數字, 要做加減運算也會變得很複雜. 所以電腦需要特殊的數字系統. 但在章節的一開始, 我們先不管電腦是如何儲存數字的, 我們先看看我們所使用的數字系統有何奧妙之處. Section 2.1, 2.2 in Version 2 Section 2.3 in Version 2 is ignored.

5 Numbering System Four numbering systems are dominant today in the world of computers: Decimal system Binary system Hexadecimal system (new) Octal system (new) They are positional number systems. Roman numerals are an example of non-positional number systems. I:1, V:5, X:10, L:50, C:100, D:500, M1000

6 Decimal System The position of number is meaningful.
Base (or radix) of the decimal system = 10 使用十進位是很自然的事, 那是因為我們有10隻手指. 現今我們使用的是阿拉伯數學家在 8 世紀發展出來 10 進位法則. 但更早在古埃及便已經在使用十進位的數字系統. 在古埃及之後的巴比倫帝國改良了埃及的數字系統, 使得數字的位置(個位數, 十位數, 百位數...) 變得有意義. 數字的位置有甚麼意義? 為什麼寫出來的數字(ex: 243), 它的 2nd position(十位數)就要代表10, 3rd position (百位數)就要代表 100, 便需要有數學的根據, 才能如此斷言. 答案在於這個數字系統的基底 base 為 10, 每個位置的值超過 10 就要進位. 換句話說, 個位數代表的是 10 的 0 次方, 十位數代表的是 10 的 10 次方,, 百位數代表的是 10 的 2 次方. 這章圖便說明 243 其實是 2*100+4*10+3*1 的意思. 如果今天使用的不是 10 進位, 基底不是 10, 數字的 2nd position 就不再代表 10 了. 10 decimal digits: 0-9

7 Decimal Real Number The following shows the place values for the real number Integral part Fractional part

8 Binary System Base of the decimal system = 2 Digits are 0 and 1.
Binary 數字系統的基底 base 為 2. 所以, 每一個位置都只能是 0 與 1, 一旦大於等於 2, 就要進位. 第 n 個位置, 代表 2 的 n-1 次方. = 24310

9 Binary Real Number The following shows that the number (101.11)2 in binary is equal to the number 5.75 in decimal. Integral part Fractional part

10 Hexadecimal System Base of the decimal system = 16
Digitals are 0, 1, …, 9, A, B, …, F. The word hexadecimal is derived from the Greek root hex (six) and the Latin root decem (ten). The equivalent decimal number is N = = 686.

11 Octal System Base of the decimal system = 8 Digitals are 0, 1, …, 8.
The equivalent decimal number is N = = 686.

12 Base 10 Base 2 1 2 10 3 11 4 100 5 101 6 110 7 111 8 1000 9 1001 1010 Section 3.2 Conversion 學習 10 進位與 2 進位間的轉換. Section 2.2 in Version 2

13 Binary to Decimal Conversion
Multiple each binary digit by its corresponding weight (i.e., power of 2)

14 Example 1 Question: Convert the binary number 110.11 to decimal.
Solution:

15 Questions 1 Convert the binary number 11111111 to decimal.
Answer: (1) 255 (2) 128+8=136

16 Properties of Binary Number
The value is multiplied by 2 if we add a 0 on the right side of binary number. The value is divided by 2 if we remove a bit on the left side of the binary number. …+ 2n-1 = 2n -1 只要 number 大於 7, 一定會用到第四位以上的數字. 1+2+4=7 < 8 1 10 2 11 3 110 6 111 7 1110 14 1111 15 11110 30 1 10 2 11 3 100 4 111 7 1000 8 1111 15 10000 16

17 Shift the Radix Point To move the radix point left one digit
The new number = the original number 2 Left movement of x positions: multiple 2-x To move the radix point right one digit The new number = the original number 2 Right movement of x positions: multiple 2x Original Number Move New Number =  = = 2 =  = = 2

18 Decimal to Binary Conversion
32<45<64 45=32+13 8<13<16 45=32+8+5 4<5<8 45= …+ 2n-1 = 2n -1 只要 number 大於 7, 一定會用到第四位以上的數字. 1+2+4=7 < 8

19 Idea of Conversion = 4510 How to get the position value a at 20? Divide by 2, if the remainder is 1, a=1; if the remainder is 0, a=0. Dividing 45 by 2 equals to shift right the number and get quotient = 2210 We treat the quotient 2210 as a new number and repeat the same method to get the position value a at 21

20 Decimal to Binary Conversion (1/2)
Divide the number continuously by 2 and write the quotient and the remainder. 原理請見下一頁說明. 每一次除以2, 其作用就如同將小數點往左移一位. 25 24 23 22 21 20

21 Decimal to Binary Conversion (2/2)
Quotient Remainders 45=  2 + 1 = (11  2 + 0)  2 + 1 = ((5  2 + 1)  2+ 0)  2 + 1 = (((2  2 + 1)  2 + 1)  2+ 0)  2 + 1 = ((((1  2 + 0)  2 + 1)  2 + 1)  2+ 0)  2 + 1 =(((((0  2 + 1)  2 + 0)  2 + 1)  2 + 1)  2+ 0)  2 + 1 = ((((1  )  2 + 1)  2 + 1)  2+ 0)  2 + 1 = 1       20  4510 =

22 Example 2 Question: Convert the decimal number 35 to binary. Solution:
35=1 24 + 023 + 022 + 121 + 120 Quotient ←1 ← 2 ← 4 ← 8 ←17←35 ↓ ↓ ↓ ↓ ↓ ↓ Remainder  3510 =

23 Changing Fractions to Decimal
We use a radix point in the same role as the decimal point. Decoding the binary 小數點稱為 radix point 或 decimal point. 從小數點向左開始數, 代表的是 2 的 0 次方, 1 次方, 2 次方.... 同理可推, 從小數點向右邊數, 代表的是 2 的 -1 次方, -2 次方.... 所以 binary 的小數很容易轉為 decimal. 在例子中為 = 也可以將分數的部份想成是numerator(分子)與denominator(分母)間的關係, 在圖中, 分數是 5/8, 分母是 8, 用 3 bits 來表示分子 101 (=5) ,

24 Changing Fractions to Binary
Repetitive multiplication: multiply the fraction by 2 (i.e., the radix point shift to right) Carry: set to 1 No carry: set to 0 若要將一個帶有小數的數轉成 binary, 在大於 0 的部份就按照一般的方式來轉換, 分數 (fraction) 的部份, 則用重覆乘以 2 的做法來求得. 其理論在於每乘上一個 2, 代表小數點往右移一位. 所以我們好像都在看小數點後的那一個 2 的 -1 次方的位置. 如果乘出來的積有進位, 表示其值大於 0.5, 所以在 2 的 -1 次方的位置要填入 1. 反之, 沒有進位, 在內 2 的 -1 次方的位置就要填入0. 剩下得數則重覆相同的動作. 以 為例, 先乘以 2 得 0.25, 仍小於 1, 所以在 2-1的位置填入 0. 接下來 0.25 乘以 2 得 0.5, 仍小於 1, 所以在 2-2的位置填入 0. 接下來 0.5 乘以 2 得 1, 所以在 2-3 的位置填入 1. 因為已經清為 0, 所以便結束了. 1/2

25 Example 3 Question: Transform the fraction 0.875 to binary. Solution:
Answer: 0.111 0.875  2     0.0

26 Example 4 Question: Transform the fraction 0.4 to a binary of 6 bits.
Solution: No exact binary representation Truncation error Answer: 0.4     2 0.4    1.2    1.6 你可以發現若用 binary 表示 0.4, 則會有循環出現, , 所以不可能用 computer 完全正確表示出 0.4, 即使 storage 再大都沒有用, 但是可讓精確度高一些, 逼近 0.4. 這樣的問題我們稱為 truncation error 或 round-off error. Truncation error: 由於項次過多, 無法計算所有項次, 導致計算到某一項次後便須捨棄其後所有的項次, 例如級次的計算. The truncation error is the difference between the original number and what is retrieved Round-off error: 在儲存位元有限下, 對實數過長的位數必須捨棄, 造成與原資料間的差異. 包括 truncation, round-up, 或 round-down 所造成的 error.

27 Question 2 Convert the decimal number 42 to binary.
Convert the decimal number 42 to octal number. Convert the decimal number 115 to hexadecimal number. Convert the binary number to hexadecimal. Answer: (1) (2) (3) O52 (4) = 73H (5) B= 1DEAH 如何從10進位轉8進位? 如何從2進位轉8進位? 8進位轉2進位?

28 Section 3.3 Integer Representation
以下要探討 integer 如何用 computer 來表示. Section 3.2 in Version 2

29 Range of Integers Whole numbers are integers.
No computer can store all the integers. The range of integers stored in computer depends on the storage size. To use computer memory more efficiently, several integer representation have been developed. Integer 是不包含小數 (fraction) 的, 也稱為 whole number. 世上的 integer 是無限多個, 不可能全部用 computer 表示出來, (會需要無限大的 storage). 這表示電腦所能表示的數字範圍, 是有一定的範圍, 範圍的大小是依據 computer 所採用的表示法, 給予每一個 integer 多大的資料空間來決定. 一個好的表示法, 應該要讓電腦的 storage 發揮空間的 efficient, 但是不可能有一種表示法含有所有的優點 (又可能使記憶體空間使用率高, 又可以表示大範圍的數字). 所以有許多不同的表示法被發展出來. 這些表示法會同時被一台 computer, 一個 program 所使用, 其原因在於我們想使電腦用較少的記憶體空間, 儲存最多我們會用到的數字. 例如如果我們只用到正數, 就不需要浪費記憶體去儲存負數. 因此為不同的數字範圍, 我們可以採用不同的表示法, 就可達到較好的空間使用率. 例如 C 要求 computer 用不同的方式來儲存 unsigned number 與 signed number. 如果你所需的數字不會太大, 就可以用 2 bytes 來儲存 integers, 而不需要用到 4 bytes, 例如 C 中就分有 long integer (32 bits) 與 integer (16 bits), 分別要求電腦用不同的記憶體空間來儲存一個 integer, unsigned integer 用 16 bits, unsigned long integer 用 32 bits. Negative numbers (0) Positive numbers (0)

30 Taxonomy of Integers Integer Representation Unsigned Signed Sign-and-
在 signed integer 的表示法中, 以底下三種表示法最常用. 其實還有 Excess representation 也會在此章介紹到. Sign-and- Magnitude One’s Complement Two’s Complement Excess System

31 Unsigned Integers Format
An unsigned integer is an integer without a sign. To store an unsigned integer in N bits: 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 total of N bits. 無號數 (unsigned integer) 就是沒有正負號的數, 換言之, 全部都是正數. 其儲存的bit string就是其數值 in the binary system.

32 Unsigned Integers N=3 N=3 0〜2N-1=23-1=7 Range: 0〜7 000 1 001 2 010 3
Base 10 Storage 000 1 001 2 010 3 011 4 100 5 101 6 110 7 111 N=3 0〜2N-1=23-1=7 Range: 0〜7

33 Unsigned Integers N=4 N=4 Range: 0〜2N-1=24-1=15 Range: 0〜15 0000 1
Base 10 Storage 0000 1 0001 2 0010 3 0011 4 0100 5 0101 6 0110 7 0111 Base 10 Storage 8 1000 9 1001 10 1010 11 1011 12 1100 13 1101 14 1110 15 1111 N=4 Range: 0〜2N-1=24-1=15 Range: 0〜15

34 Range of Unsigned Integers
The range depends on the number N of bits the computer allocates to store an unsigned integer (to store its binary number). Range: 0 〜 2N-1 Number of Bits Range Maximum unsigned integer 〜 = 24-1 〜 = 28-1 〜65, ,535 =

35 Overflow The term overflow describes a condition in which a number is not within the range defined by the allocation. Decimal 8-bit Allocation bit Allocation overflow 24, overflow 1,245, overflow overflow 2 台不同的機器, 分別以 8 bits 與 16 bits 來儲存 unsigned number. 就可以看到有其極限: 8-bit allocation: 0-255 16-bit allocation: 0-65,535

36 Example 5 Question: Store 7 in an 8-bit memory location. Solution:
 1112 2. Add five 0s to left Result: 1 1 1

37 Example 6 Question: Store 258 in a 16-bit memory location. Solution:
2. Add seven 0s to left Result: If you store 258 in an 8-bit memory location, overflow occurs.

38 Questions 3 Store 65 in a 8-bit memory location.
Answer: (1) (2)

39 Interpretation of Unsigned Integers
How do you interpret an unsigned representation in decimal? Use binary to decimal conversion method and get the result.

40 Example 7 Question: Interpret in decimal if the number was stored as an unsigned integer. Solution: Binary Weights Decimal =43

41 Applications of Unsigned Integers Format
Unsigned integers representation can improve the efficiency of storage because you need not to store the sign of the integer. Cases of applications: Counting Addressing Storing other data type: text, images, audio, video 在 unsigned integer representation 中, 所有的 bit 都是來儲存數字, 無需儲存 sign, 因此這是一個很有效率的表示法. 無號數的應用如 counting (數有多少人進入博物館參觀, 只有正數!), addressing (記錄電腦中記憶體的號碼, 如第幾個 byte) 通常電話號碼或住址號碼我們會直接使用 ASCII 來表示.

42 Signed Integer Sign-and-magnitude One’s complement Two’s complement
Excess system (See Section 3.4) When we use a 4-bit allocation to store signed integers, the available range is divided into two parts: positive and negative integers. How to design your own representation? 如何切成 positive and negative parts? 各一半, 擁有一樣多的數目的 bit pattern? 如何很容易地將 integer 轉換成 bit pattern, 而且看到 bit pattern 也很容易可以轉回 integer. 轉成電腦可以儲存的格式後, 是否可以很容易的進行算數運算?

43 Sign-and-Magnitude Format (1/2)
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. The rest N-1 bits define the magnitude of the number. Magnitude: the absolute value of the number To store the binary representation of its absolute value.

44 Sign-and-Magnitude Format (2/2)
1 Decimal: Sign-and-magnitude: (or 08H) 在 sign-and-magnitude 表示法中, 最左邊的 bit 表示 sign, 其他的 bit 才用於表示 number. 1 1 Decimal: Sign-and-magnitude: (or 88H)

45 Sign-and-Magnitude N=3
Base 10 storage 000 1 001 2 010 3 011 -0 100 -1 101 -2 110 -3 111 N=3 2N-1-1=22-1=3 Range: -3〜3

46 Sign-and-Magnitude N=4
Base 10 Storage 0000 1 0001 2 0010 3 0011 4 0100 5 0101 6 0110 7 0111 Base 10 Storage -0 1000 -1 1001 -2 1010 -3 1011 -4 1100 -5 1101 -6 1110 -7 1111 N=4 2N-1-1=23-1=7 Range: -7〜7

47 Representation of Sign-and-Magnitude Integers
To store a signed integer in N bits: 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 binary number so that there is total of N-1 bits. 3. If the number is positive, 0 is added to the leftmost significant bit. 4. If the number is negative, 1 is added to the leftmost significant bit.

48 Range of Sign-and-Magnitude Format
Range of N-bit memory location: Range: - (2N-1-1) 〜 + (2N-1-1) 1 N-1 bits Number of Bits Range 〜 (1111〜0111) 〜127 ( 〜 ) ,767〜32,767 ,147,483,647〜2,147,483,647 Decimal 8-bit Allocation bit Allocation overflow -24, overflow

49 Positive and Negative 0 There are two 0s in sign-and-magnitude representation: positive 0 and negative 0. In an 8-bit allocation: + 0 → - 0 →

50 Example 8 Question: Store +28 and -28 in an 8-bit memory location using sign-and-magnitude representation. Solution:   2. Add two 0s to make 7 bits: 3. Add the sign bit: Result=   3. Add the sign bit: Result=

51 Example 9 Question: Store -258 in an 16-bit memory location using sign-and-magnitude representation. Solution:   2. Add six 0s to make 15 bits: 3. Add the sign bit: Result=

52 Interpretation of Sign-and-Magnitude Integers in Decimal
How do you interpret an sign-and-magnitude representation in decimal? 1. Ignore the leftmost bit. 2. Change the N-1 bits from binary to decimal. 3. Attach a + sign to the number if the leftmost bit =0. 4. Attach a – sign to the number if the leftmost bit =1.

53 Example 10 Question: Interpret in decimal if the number was stored as a sign-and-magnitude integer. Solution: 1. Ignore the left most bit  2. Change to decimal  5910 3. Add – sign since the leftmost bit =1 Result= - 59

54 Applications of Sign-and-Magnitude Format
It is not really used to store signed numbers. Disadvantage: Operations are not easy. There are two 0’s. Advantage: Transfer from decimal to binary, and vice versa, is very easy. Cases of applications: When we quantize an analog signal, such as audio, the sign-and-magnitiude representation is used. 在 computer 中, 並沒有使用 sign-and-magnitude 的方式來儲存 signed integer. 原因有兩點: 1. 在 sign-and-magnitude integers 上做加減等運算, 不是件簡單直覺的工作. 在後面的章節, 會教到加法, 若用 sign-and-magnitude, 如 +4 加上 -4, ( ) + ( ) = 不容易計算. 2. 在 sign-and-magnitude 表示法中會出現 2 個 0, 會令 programmer 覺得不舒服. 但是 sign-and-magnitude 也有個優點: 在 decimal 與 binary 間的轉換非常容易. 所以如果應用程式不需做運算, 則 sign-and-magnitude 也是不錯的方法. 所以可以應用於 sampling 上, 將 analog signal 轉成 digital signal.

55 Questions 4 Store -64 in an 8-bit memory location using sign-and-magnitude representation. Interpret in decimal if the number was stored as a sign-and-magnitude integer. Answer: (1) (2) -33

56 One’s Complement Format (1/2)
To store an positive integer K in N bits: Change K to binary format. 0’s are added to the left to the number to make a total of N bits. 要分清楚 representation 與 operation 是不一樣的. Decimal: 8  K=8:  K=8: 1 One’s complement: 08H

57 One’s Complements Format (2/2)
To store an negative integer -K in N bits: 1. Change K to binary format in N bits 2. Complement it. Complement: change all 0s to 1s and all 1s to 0s. Above operation is called one’s complementing an integer. 從另一個角度看, 當一個數值 (-k <0 )時, 其 1’s complement 表示法為 2^(N) -1 -k , 其原因是做 complement, 就好像用 減去 k 的 binary. Decimal:  K=8:  complement 1 1 1 1 1 1 1 One’s complement: F7H

58 1’s Complement N=3 N=3 2N-1-1=22-1=3 Range: -3〜3 000 1 001 2 010 3 011
Base 10 storage 000 1 001 2 010 3 011 -3 100 -2 101 -1 110 -0 111 N=3 2N-1-1=22-1=3 Range: -3〜3

59 1’s Complement N=4 N=4 2N-1-1=23-1=7 Range: -7〜7 0000 1 0001 2 0010 3
Base 10 Storage 0000 1 0001 2 0010 3 0011 4 0100 5 0101 6 0110 7 0111 Base 10 Storage -7 1000 -6 1001 -5 1010 -4 1011 -3 1100 -2 1101 -1 1110 -0 1111 N=4 2N-1-1=23-1=7 Range: -7〜7

60 Example 11 Question: Store –258 in a 16-bit memory location using one’s complement representation. Solution:   2. Add seven 0s to make 16 bits: 3. Complement: Result=

61 Range of One’s Complement
Range of N-bit memory location: Range: - (2N-1-1) 〜 + (2N-1-1) N bits Number of Bits Range 〜 (1000〜0111) 〜127 ( 〜 ) ,767〜32,767 ,147,483,647〜2,147,483,647

62 Features of 1’s Complement (1/2)
In one’s complement 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. There are two 0s in one’s complement representation: positive 0 and negative 0. In an 8-bit allocation: + 0 → - 0 →

63 Features of 1’s Complement (2/2)
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. Ex: = -310 v.s = 310 1’s complement 代表顛倒所有的 bit (1 變 0, 0 變 1). 如果你將一個正數 k 做 complement, 則會得到相對的負數 (-k). 如果你將一個負數 -k 做 complement, 則會得到相對的正數 (k). 如果你將一個數 k 做兩次 complement, 則會得原來的數 (k).

64 Interpretation of 1’ Complement Integers in Decimal
How do you interpret a one’s complement representation in decimal? 1. If the leftmost bit is 0: (positive) 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) a. Complement the entire number. b. Change the entire number from binary to decimal. C. Put a negative sign - in front of the number.

65 Example 12 Question: Interpret in decimal if the number was stored as a one’s complement integer. Solution: is negative  (complement) 2. Change to decimal  910 3. Add – sign since the leftmost bit =1 Result= - 9

66 Applications of One’s Complement Format
It is not used to store signed numbers. Disadvantage: Operations are not easy. There are two 0’s. Advantage: It is the base of 2’s complement. It is interesting for data communication applications such as error detection and correction. 在 computer 中, 並沒有使用 1’s complement 的方式來儲存 signed integer. 原因有兩點: 1. 在 1’s complement integers 上做加減等運算, 不是件簡單直覺的工作. 有正 0 與負 0, 會造成一些混淆, 如 4 加負0 會得到 ( ) + ( ) = 2. 在 1’s complement 表示法中會出現 2 個 0, 會令 programmer 覺得不舒服. 如 +4 加上 -4, ( ) + ( ) = = 負 0. 但是 one’s complement 也有個優點: 1’s complement 是 2’s complement 的基礎. 1’s complement 有一些特性, 可用於資料錯誤偵測與更正等資料傳輸等應用.

67 Questions 5 Store -64 in an 8-bit memory location using 1’s complement representation. Interpret in decimal if the number was stored as a 1’s complement integer. Answer: (1) (2) -94

68 Two’s Complement Format (1/3)
Two’s complement is the most common, the most important, and the most widely used representation of integers today. To store an positive integer K in N bits: Change K to binary format. 0’s are added to the left to the number to make a total of N bits. 2’s complement 會解決 1’s complement 所有的問題. Decimal: 6  K=6:  K=6: 1 1 Two’s complement: 06H

69 Two’s Complements Format (2/3)
To store an negative integer -K in N bits: 1. Find the one’s complement of -K. 2. Add 1 to its one’s complement. Decimal:  K=6:  1’s complement  add 1 to it  2’s complement 從另一個角度看, 當一個數值 (-k <0 )時, 其 2’s complement 表示法為 2^(N) -k , 其原因是做 complement, 就好像用 (=255)減去 k 的 binary, 最後再加上 1. 1 1 1 1 1 1 Two’s complement: FAH

70 Two’s Complements Format (3/3)
Another method to store an negative integer -K in N bits : 1. Ignore the sign. Change K to binary of N bits. 2. Leave all the rightmost 0’s and the first 1 unchanged. 3. Complement the rest of the bits. The above operation (in 3/2, 3/3) is called two’s complementing an integer.

71 Representation -6 (1) (2) (3) (4)

72 2’s Complement N=3 N=3 2N-1-1=22-1=3 Range: -4〜3
Base 10 storage 000 1 001 2 010 3 011 -4 100 -3 101 -2 110 -1 111 N=3 2N-1-1=22-1=3 Range: -4〜3 The most significant bit works as a sign bit. 0: positive 1: negative

73 2’s Complement N=4 N=4 2N-1-1=23-1=7 Range: -8〜7 0000 1 0001 2 0010 3
Base 10 Storage 0000 1 0001 2 0010 3 0011 4 0100 5 0101 6 0110 7 0111 Base 10 Storage -8 1000 -7 1001 -6 1010 -5 1011 -4 1100 -3 1101 -2 1110 -1 1111 N=4 2N-1-1=23-1=7 Range: -8〜7

74 Example 13 Question: Store –40 in a 8-bit memory location using two’s complement representation. Solution:   2. Add two 0s to make 8 bits: 3. 1’complement 4. Add 1 to it: Answer: Another Method 2.complement 1. copy

75 Two’s Complement Format
Range of N-bit memory location: Range: - (2N-1) 〜 + (2N-1-1) Number of Bits Range 〜 (1000〜0111) 〜127 ( 〜 ) ,768〜32,767 ,147,483,648〜2,147,483,647

76 Feature of 2’s Complement
There is only one 0 in two’s complement representation. In an 8-bit allocation: + 0 → If you two’s complement a positive number, you get the corresponding negative number. If you two’s complement a negative number, you get the corresponding positive number. If you two’s complement a number twice, you get the original number. 如果你將一個正數 k 做 2‘s complement, 則會得到相對的負數 (-k). 如 010 (=2) 做 ‘2’s complement, 會得到 110 (= -2) 如果你將一個負數 -k 做 2‘s complement, 則會得到相對的正數 (k). 如 110 (=-2) 做2‘s complement, 會得到 010 (= 2) 如果你將一個數 k 做兩次 2’s complement, 則會得原來的數 (k).

77 Interpretation of 2’s Complement Integers in Decimal
How do you interpret a two’s complement representation in decimal? 1. If the leftmost bit is 0: (positive) 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) a. Minus the entire number by 1. b. Complement it. c. Change the entire number from binary to decimal. d. Put a negative sign - in front of the number.

78 Example 14 Question: Interpret in decimal if the number was stored as a two’s complement integer. Solution:  minus 1: 2. Complement it:  3. Change to decimal:  2610 4. Add - to it: Answer: -26

79 Questions 6 Store -64 in an 8-bit memory location using 2’s complement representation. Interpret in decimal if the number was stored as a 2’s complement integer. Answer: (1) (2) -95

80 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 +0 +1 +2 +3 +4 +5 +6 +7 -7 -6 -5 -4 -3 -2 -1 -0 Two’s Complement +0 +1 +2 +3 +4 +5 +6 +7 -8 -7 -6 -5 -4 -3 -2 -1

81 Section 3.4 Excess System

82 Excess System A magic number (or bias) is add to each number such that the sum is positive and can be change to binary as unsigned number. Ex: 8-bit storage Range -100〜155 → magic number = 100 Excess_100 Range -128〜127 → magic number = 128 Excess_128 Range -127〜128 → magic number = 127 Excess_127 Store the exponential value of a fraction. Excess system 只用於 floating-point 表示法中, 表示指數 (exponential value) 的部份. 作法是將每個 exponential value 加上一個很大的數, 稱為 magic number, 使得在表示的範圍內, 所有的數都變成正數, 在用一般 unsigned number 表示法, 轉成 binary 即可. 對於 8-bit storage 的範圍, 通常想表示的範圍是 -128 〜 127, 所以若加上 128 就可使所有的 integer 變成 positive 0〜 255. 再轉換成 binary.如果想表示的範圍是 -127 〜 128, magic number=127. 如果想表示的範圍是 -100 〜 155, magic number=100.

83 Excess_3 N=3 2N =8 number Range: -3〜4 Magic Number = 3 -3 000 -2 1 001
Base 10 + Magic number Storage -3 000 -2 1 001 -1 2 010 3 011 4 100 5 101 6 110 7 111 N=3 2N =8 number Range: -3〜4 Magic Number = 3 1. 先決定 bit 數 2. 決定要表示的數字範圍 3. 得到 magic number =3 = Min in the range * (-1) 4 轉換時, 先加上 magic number 後再去做 binary 的轉換 (所有的數字都會落在 0-7)

84 Excess_127 To store an integer K in N=8 bits: Magic number = 127
Add magic number to K (i.e., K=K+127) The range is –127〜128 Change K to binary format. Decimal: K=8 , N=8  magic number =  K=K+127 =8+127=135  K=135: 1 1 1 1 Excess-127 representation: 87H

85 Excess_128 To store an integer K in N=8 bits: Magic number = 128
Add magic number to K (i.e., K=K+128) The range is –128〜127 Change K to binary format. Decimal: K=8 , N=8  magic number =  K=K+128 =8+128=136  K=136: 1 1 Excess-128 reprsentation:88H

86 Example 15 Question: Represent –25 in Excess_127 using an 8-bit allocation. Solution: 1. magic number = 127 = 102 3. Change to binary:  Answer

87 Interpretation of Excess Integer in Decimal
How do you interpret an excess integer representation in decimal? 1. Change the number to decimal. 2. Subtract the magic number from the integer.

88 Example 16 Question: Interpret if the representation is Excess_127. Solution: 1. Change to decimal:  25410 2. Subtract 127 to it: Answer: 127

89 Questions 7 Store -64 in an 8-bit memory location using Excess_127 representation. Store -64 in an 8-bit memory location using Excess_128 representation. Interpret in decimal if the number was stored as an Excess_128 integer. Answer: (1) (2) (3) 33 Question: 如果要用9 bits 表示最小為 -32 的連續 integer, 則要使用? Answer: (1) Excess-32 (2) magic number=32 (3) range=

90 Section 3.5 Floating-Point Representation

91 Floating-point A floating-point number contains integer and fraction.
To represent a number, for example, ( ) Store the sign, all of the bits, and the position of the decimal point in memory. Floating-point 浮點數 float 代表小數點是可以往右或往左浮動, 不管是小數部份的 digits 比較多還是整小數部份的 digits 比較多. 如果要儲存一個像 ( ) 這樣的數在電腦中, 你必須在記憶體儲存 sign, all of the bits, 和小數點的位置. 雖然做得到, 但這會變得很複雜, 特別是在對這種表示法的數字做加減等運算. 所以有一種標準的表示法, 稱為 floating-point representation.

92 Scientific Notation of Decimal
To move the radix point so that there only one digit to the left of the decimal point Right/left movement of x positions: multiple 10-x/10x Original Number Move Normalized   8.133     Exponent fixed-point section

93 Scientific Notation of Binary
To move the radix point so that there only one 1 to the left of the decimal point Right / left movement of x positions: multiple 2-x/2x Original Number Move Normalized   1.111         我們將數字做 normalization, 變成 1.xxxxxxx 乘上 2 的 power, 換句話說, 就是其科學表示法, 表示成指數與. 可以使得記錄小數點的位置的表示, 轉換成記錄指數的值. Exponent mantissa

94 IEEE Standards for Floating-point Representation
IEEE: The Institute of Electrical and Electronics Engineers IEEE 定義了三種儲存 floating-point 的標準. 其中有兩種是用於儲存於 memory 中, 分別是 single precision 與 double precision. 在 C 語言中, floating point number 分成 float 與 double 兩種, 分別用 32 bits 與 64 bits 來儲存 floating point.

95 Examples of Floating-point Representation
Single-precision representation Store the sign as 0 (positive) or 1 (negative). Store the exponent (power of 2) as Excess_127. Store the mantissa as an unsigned integer Number Sign Exponent Mantissa -22  +2-6  -2-3 

96 Example 17 To store a number 3.75 in 8 bits: 0 1 0 0 1 1 1 0
Change to binary format Normalization to +21  Sign: 0 for positive, 1 for negative Exponent: Excess_3 Floating-point 的計算法各家都多多少少有不一樣的地方, 使用的位元數也許不同, 但意義都是一樣的, Positive number, 所以 sign bit=0 因為使用 3 個 bit 來表示 exponent, 所以 magic number = =3, 所以 1+ magic number=4, 存成 100 Exponent 決定浮點數的能表示的範圍 (range) Mantissa 的定義為小數點後面的 binary number. 小數點的左邊都是 1, 所以可以忽略不寫. 就變成 1110. Mantissa 決定浮點數的精確度 (precision), 如果有小數部份因 mantissa 的長度不夠而刪去, 稱為 truncation error. Ex: 1/3, 1/10 都會有 infinite terms.

97 Example 18 Question: To store a number -8.125 in 8 bits. Solution:
Change to binary format Normalization to -23  Negative: sign = 1; Excess_3: 3+3=6 在這個例子中, 有 truncation error, 因為儲存的位置不足, 所以反求得到的數字是 8.

98 Example 19 Question: Show the representation of the normalized number Solution: → → 27 × Exponent=7 → 7+127=134 → For → Mantissa = Sign Exponent (8 bits) Mantissa (23 bits)

99 Interpretation of Floating-point Number to Decimal
Interprets a 32-bit floating-point number: 1. Use the left most 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. 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.

100 Example 20 Question: Interpret the following 32-bit floating-point number Solution: Sign=1: negative Exponent= =12410 → = -3 Answer =  = =

101 Questions 7 Show the floating-point representation of the decimal number Show the floating-point representation of the decimal number 0. Interpret the following 8-bit floating-point number in decimal. Answer: (1) (2) –1.75 Excess_3

102 Storing Zero A real number zero, 0.0, cannot be stored using the steps discussed above. In this special case, the sign, exponent and the mantissa are set to 0s.

103 Overflow and Underflow in Floating-point Representation
An attempt to store numbers with very small absolute values results in an underflow condition. An attempt to store numbers with very large absolute values results in an overflow condition. +129 +129 ( ) ( )

104 Section 3.6 Hexadecimal Notation

105 Hexadecimal Notation Hexadecimal notation provides a convenient way for human reading. → x30 or 30H

106 Section 3.7 Key Terms

107 Key Terms (1/2) How many terms can you describe? Decimal system
Binary system Decimal to binary conversion Binary to decimal conversion Whole number Integer Fraction Unsigned integer Positive integer Negative integer Whole number: 整數的意思 Integer 指整數, 或一個數的整數部份: integer part. Fraction 指分數, 或一個數的小數部份: fraction part

108 Key Terms (2/2) How many terms can you describe?
Sign-and-magnitude representation One’s complement representation Two’s complement representation Excess system Floating-point number Normalization Mantissa Exponent Single-precision format Double precision format

109 Section 3.8 Summary

110 Summary (1/5) 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. Unsigned numbers are commonly used for counting and addressing.

111 Summary (2/5) There are three major methods of signed number representations: sign-and-magnitude, one’s complements, and two’s complement. In the sign-and-magnitude method of integer representation, 1 bit represents the sign of the number; the remaining bits represent the magnitude. In one’s complement method of integer representation, a negative number is represented by complementing the corresponding positive number.

112 Summary (3/5) Complementing a number means to convert each 1 to 0 and 0 to 1. In the two’s complement methods of integer representation, a negative number is represented by leaving all the rightmost 0s and the first 1 unchanged an then complementing the remaining bits. Most computers today used the two’s complement method of integer representation.

113 Summary (4/5) 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 number and a fraction. Conversion of the fraction to binary requires the denominator of the fraction to be expressed as power of 2. The Excess_X system is used to store this power of 2.

114 Summary (5/5) A fraction is normalized so that operations are simpler.
To store a fraction in memory, you need its sign, exponent, and mantissa.

115 Homework Review Questions: 7,10,13,14,15,17
Multiple-Choice Questions: 20-46 Exercises: 47(a), 49(a,b), 52(a,b), 53(a,c), 65(a,c), 66, 70(a),74(b,c) 請說明你對利用peer-to-peer技術的看法。 peer-to-peer: 如下載 mp3, Skype等應用.


Download ppt "Chapter 3 Number Representation"

Similar presentations


Ads by Google