Chapter 3 Number Representation

Slides:



Advertisements
Similar presentations
Lecture - 2 Number systems and computer data formats
Advertisements

©Brooks/Cole, 2003 Chapter 3 Number Representation.
Assembly Language and Computer Architecture Using C++ and Java
Number Systems Standard positional representation of numbers:
Chapter 4 Operations on Bits
Signed Numbers.
Assembly Language and Computer Architecture Using C++ and Java
Data Representation ICS 233
S. Barua – CPSC 240 CHAPTER 2 BITS, DATA TYPES, & OPERATIONS Topics to be covered are Number systems.
Number Representation Rizwan Rehman, CCS, DU. Convert a number from decimal to binary notation and vice versa. Understand the different representations.
Floating Point Numbers.  Floating point numbers are real numbers.  In Java, this just means any numbers that aren’t integers (whole numbers)  For example…
Number Systems Lecture 02.
Operations on data CHAPTER 4.
4 Operations On Data Foundations of Computer Science ã Cengage Learning.
Chapter 3 Data Representation part2 Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
Binary Representation and Computer Arithmetic
Dr. Bernard Chen Ph.D. University of Central Arkansas
Chapter3 Fixed Point Representation Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009.
The Binary Number System
Data Representation Number Systems.
Simple Data Type Representation and conversion of numbers
ES 244: Digital Logic Design Chapter 1 Chapter 1: Introduction Uchechukwu Ofoegbu Temple University.
Chapter 3 Number Representation. Convert a number from decimal 、 hexadecimal,octal to binary notation and vice versa. Understand the different representations.
Computer Arithmetic Nizamettin AYDIN
#1 Lec # 2 Winter EECC341 - Shaaban Positional Number Systems A number system consists of an order set of symbols (digits) with relations.
1 Digital Technology and Computer Fundamentals Chapter 1 Data Representation and Numbering Systems.
1 Lecture 5 Floating Point Numbers ITEC 1000 “Introduction to Information Technology”
NUMBER REPRESENTATION CHAPTER 3 – part 3. ONE’S COMPLEMENT REPRESENTATION CHAPTER 3 – part 3.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Information Representation: Negative and Floating Point.
Chapter 1 Data Storage(3) Yonsei University 1 st Semester, 2015 Sanghyun Park.
IT253: Computer Organization
Studies in Big Data 4 Weng-Long Chang Athanasios V. Vasilakos MolecularComputing Towards a Novel Computing Architecture for Complex Problem Solving.
Computer Architecture
Dale & Lewis Chapter 3 Data Representation. Data and computers Everything inside a computer is stored as patterns of 0s and 1s Numbers, text, audio, video,
Lec 3: Data Representation Computer Organization & Assembly Language Programming.
Number Systems Spring Semester 2013Programming and Data Structure1.
CH09 Computer Arithmetic  CPU combines of ALU and Control Unit, this chapter discusses ALU The Arithmetic and Logic Unit (ALU) Number Systems Integer.
Data Representation Dr. Ahmed El-Bialy Dr. Sahar Fawzy.
Lecture 4 Last Lecture –Positional Numbering Systems –Converting Between Bases Today’s Topics –Signed Integer Representation Signed magnitude One’s complement.
Lecture 5. Topics Sec 1.4 Representing Information as Bit Patterns Representing Text Representing Text Representing Numeric Values Representing Numeric.
©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.
CSC 221 Computer Organization and Assembly Language
Number Representation
©Brooks/Cole, 2003 Chapter 3 Number Representation.
1 Representation of Data within the Computer Oct., 1999(Revised 2001 Oct)
Computer Math CPS120 Introduction to Computer Science Lecture 4.
Computer Organization and Design Information Encoding II Montek Singh Wed, Aug 29, 2012 Lecture 3.
IT1004: Data Representation and Organization Negative number representation.
©Brooks/Cole, 2003 Chapter 3 Number Representation.
Data Representation COE 301 Computer Organization Dr. Muhamed Mudawar
Computer Math CPS120 Introduction to Computer Science Lecture 7.
Number Systems. The position of each digit in a weighted number system is assigned a weight based on the base or radix of the system. The radix of decimal.
Binary Numbers The arithmetic used by computers differs in some ways from that used by people. Computers perform operations on numbers with finite and.
Data Representation COE 301 Computer Organization Prof. Muhamed Mudawar College of Computer Sciences and Engineering King Fahd University of Petroleum.
1 CE 454 Computer Architecture Lecture 4 Ahmed Ezzat The Digital Logic, Ch-3.1.
MATH Lesson 2 Binary arithmetic.
Data Representation COE 308 Computer Architecture
Data Representation ICS 233
Data Representation.
Binary Numbers The arithmetic used by computers differs in some ways from that used by people. Computers perform operations on numbers with finite and.
Chapter 3 Data Storage.
Data Structures Mohammed Thajeel To the second year students
Number Representation
Chapter 3 DataStorage Foundations of Computer Science ã Cengage Learning.
OBJECTIVES After reading this chapter, the reader should be able to :
Data Representation COE 308 Computer Architecture
Presentation transcript:

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 支手指.

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.

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

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.

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

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

Decimal Real Number The following shows the place values for the real number +24.13. Integral part Fractional part

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

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

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 = 512 + 160 + 14 = 686.

Octal System Base of the decimal system = 8 Digitals are 0, 1, …, 8. The equivalent decimal number is N = 512 + 128 + 40 + 6 = 686.

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

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

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

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

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. 20 + 21 +…+ 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

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 5.7510 = 101.112 1 10.1112 = 2.87510 = 5.75102 5.7510 = 101.112 1 1011.12 = 11.510 = 5.75102

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

Idea of Conversion 1011012= 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 101102= 2210 We treat the quotient 2210 as a new number and repeat the same method to get the position value a at 21

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

Decimal to Binary Conversion (2/2) Quotient Remainders 45= 22  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 + 0)  2 + 1)  2 + 1)  2+ 0)  2 + 1 = 1  25 + 0  24 + 1 23 + 1  22 + 0  21 + 1  20  4510 = 1011012

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

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

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. 剩下得數則重覆相同的動作. 以 0.125 為例, 先乘以 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

Example 3 Question: Transform the fraction 0.875 to binary. Solution: Answer: 0.111 0.8752 0.752 0.52 0.875  1.750  1.5  1.0  0.0 0 . 1 1 1

Example 4 Question: Transform the fraction 0.4 to a binary of 6 bits. Solution: No exact binary representation Truncation error Answer: 0.011001 0.42 0.82 0.62 0.22 0.42 0.82 0.4  0.8  1.6  1.2  0.4  0.8  1.6 0 . 0 1 1 0 0 1 你可以發現若用 binary 表示 0.4, 則會有循環出現, 0.011001100110011..., 所以不可能用 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.

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 1110111101010 to hexadecimal. Answer: (1) 101010 (2) 1110011 (3) O52 (4) 111 0011= 73H (5) 1 1101 1110 1010B= 1DEAH 如何從10進位轉8進位? 如何從2進位轉8進位? 8進位轉2進位?

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

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)

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

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.

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

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

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 4 0〜15 15 = 24-1 8 0〜255 255 = 28-1 16 0〜65,535 65,535 = 216 -1

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

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

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

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

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

Example 7 Question: Interpret 00101011 in decimal if the number was stored as an unsigned integer. Solution: Binary 0 0 1 0 1 0 1 1 Weights 128 64 32 16 8 4 2 1 ------------------------------------------------------------- 32 + 0 + 8 + 0 + 2 + 1 Decimal =43

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 來表示.

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. 轉成電腦可以儲存的格式後, 是否可以很容易的進行算數運算?

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.

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

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

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

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.

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 4 -7〜7 (1111〜0111) 8 -127〜127 (1111 1111〜0111 1111) 16 -32,767〜32,767 32 -2,147,483,647〜2,147,483,647 Decimal 8-bit Allocation 16-bit Allocation 7 00000111 00000000 00000111 -124 11111100 10000000 11111100 258 overflow 00000001 00000010 -24,760 overflow 11100000 10111000

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

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

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

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.

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

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, (+ 00000100) + (10000100) = 1000 1000 不容易計算. 2. 在 sign-and-magnitude 表示法中會出現 2 個 0, 會令 programmer 覺得不舒服. 但是 sign-and-magnitude 也有個優點: 在 decimal 與 binary 間的轉換非常容易. 所以如果應用程式不需做運算, 則 sign-and-magnitude 也是不錯的方法. 所以可以應用於 sampling 上, 將 analog signal 轉成 digital signal.

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

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: 1000  K=8: 0000 1000 1 One’s complement: 08H

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, 就好像用 1111 1111 減去 k 的 binary. Decimal: -8  K=8: 0000 1000  complement 1111 0111 1 1 1 1 1 1 1 One’s complement: F7H

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

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

Example 11 Question: Store –258 in a 16-bit memory location using one’s complement representation. Solution: 1. -25810  25810  1 0000 00102 2. Add seven 0s to make 16 bits: 0000 0001 0000 0010 3. Complement: Result= 1111 1110 1111 1101

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

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 → 00000000 - 0 → 11111111

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

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.

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

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 會得到 (+ 00000100) + (-11111111) =1 0000 0011 2. 在 1’s complement 表示法中會出現 2 個 0, 會令 programmer 覺得不舒服. 如 +4 加上 -4, (+ 00000100) + (-11111011) = 1111 1111 = 負 0. 但是 one’s complement 也有個優點: 1’s complement 是 2’s complement 的基礎. 1’s complement 有一些特性, 可用於資料錯誤偵測與更正等資料傳輸等應用.

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

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: 110  K=6: 0000 0110 1 1 Two’s complement: 06H

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: -6  K=6: 0000 0110  1’s complement 1111 1001  add 1 to it  2’s complement 1111 1010 從另一個角度看, 當一個數值 (-k <0 )時, 其 2’s complement 表示法為 2^(N) -k , 其原因是做 complement, 就好像用 1111 1111 (=255)減去 k 的 binary, 最後再加上 1. 1 1 1 1 1 1 Two’s complement: FAH

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.

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

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

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

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

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

Feature of 2’s Complement There is only one 0 in two’s complement representation. In an 8-bit allocation: + 0 → 00000000 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).

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.

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

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

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

Section 3.4 Excess System

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.

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)

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 = 127  K=K+127 =8+127=135  K=135: 1000 0111 1 1 1 1 Excess-127 representation: 87H

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 =128  K=K+128 =8+128=136  K=136: 1000 1000 1 1 Excess-128 reprsentation:88H

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

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.

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

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 10100001 in decimal if the number was stored as an Excess_128 integer. Answer: (1) 00111111 (2) 01000000 (3) 33 Question: 如果要用9 bits 表示最小為 -32 的連續 integer, 則要使用? Answer: (1) Excess-32 (2) magic number=32 (3) range=-32 - 479

Section 3.5 Floating-Point Representation

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

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 81.33 1 +101  8.133 0.00000194003 6 +10-6  1.94003 -2990.001 3 -103  2.990001 Exponent fixed-point section

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 11.11 1 +21  1.111 +1010001.11001 6 +26  1.01000111001 -111.000011 2 -22  1.11000011 +0.00000111001 6 +2-6  1.11001 -0.001110011 6 -2-3  1.110011 我們將數字做 normalization, 變成 1.xxxxxxx 乘上 2 的 power, 換句話說, 就是其科學表示法, 表示成指數與. 可以使得記錄小數點的位置的表示, 轉換成記錄指數的值. Exponent mantissa

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.

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  1.11000011 1 10000001 1100001 10000000 00000000 +2-6  1.11001 0 01111001 1100100 00000000 00000000 -2-3  1.110011 1 01111100 1100110 00000000 00000000

Example 17 To store a number 3.75 in 8 bits: 0 1 0 0 1 1 1 0 Change 3.75 to binary format 11.11. Normalization to +21  1.111. Sign: 0 for positive, 1 for negative Exponent: Excess_3 Floating-point 的計算法各家都多多少少有不一樣的地方, 使用的位元數也許不同, 但意義都是一樣的, Positive number, 所以 sign bit=0 因為使用 3 個 bit 來表示 exponent, 所以 magic number = 23-1 -1=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. 0 1 0 0 1 1 1 0

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

Example 19 Question: Show the representation of the normalized number - 161.875. Solution: -161.875 → -10100001.111 → 27 × 1.0100001111 Exponent=7 → 7+127=134 → 1000 0110 For 1.0100001111 → Mantissa = 010 0001 111 1 1000 0110 010 0001 1110 0000 0000 0000 Sign Exponent (8 bits) Mantissa (23 bits)

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.

Example 20 Question: Interpret the following 32-bit floating-point number 1 01111100 11001100000000000000000 Solution: Sign=1: negative Exponent=011111002=12410 →124 - 127= -3 Answer = - 2-31.110011 = -0.001110011 = -0.22265625

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

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.

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 (1 + 2-23 ) (1 + 2-23 )

Section 3.6 Hexadecimal Notation

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

Section 3.7 Key Terms

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

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

Section 3.8 Summary

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.

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.

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.

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.

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.

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等應用.