Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Week 3: Data Representation: Negative Numbers READING: Chapter 3.

Similar presentations


Presentation on theme: "1 Week 3: Data Representation: Negative Numbers READING: Chapter 3."— Presentation transcript:

1 1 Week 3: Data Representation: Negative Numbers READING: Chapter 3

2 Data representation 2 How do we represent data in a computer system? Numbers Negative numbers Text Audio Images and graphics Video EECS 1520 -- Computer Use: Fundamentals

3 Number systems 3 Sign-Magnitude Representation in base 10: Examples : + 5- 6 Sign indicates positiveMagnitude is 5Sign indicates negativeMagnitude is 6 In Binary Systems, we can use the most significant bit to represent the “sign” Examples: 00101 is +510101 is -5 The problem is: 00000 is + 0 10000 is - 0 The two representations of zero within a computer can cause unnecessary complexity. How do we represent negative numbers in a computer system? EECS 1520 -- Computer Use: Fundamentals

4 Number systems 4 Fixed-size Sign-magnitude Let’s go back to the example with base 10. If we allow only a fixed number of values, we can represent numbers as just integer values, where half of them represent negative numbers. To perform addition, we just add the numbers together and discard any carry. EECS 1520 -- Computer Use: Fundamentals

5 Number systems 5 Signed-magnitudeFixed-size Signed-magnitudeFixed-size Examples: Fixed-size Sign-magnitude Note: “1” is the carrier and is discarded EECS 1520 -- Computer Use: Fundamentals

6 Number systems 6 So far we used the number line to calculate the negative representation of a number. Fixed-size Sign-magnitude In general, a formula that computes the negative representation is: B = Base; I = integer; k = number of digits From the above number line, to calculate the negative representation for -2: Example: EECS 1520 -- Computer Use: Fundamentals

7 Number systems 7 Apply the same formula : How about “-4”, “-5”, “-6” … “-50”? Fixed-size Sign-magnitude EECS 1520 -- Computer Use: Fundamentals

8 Number systems 8 Fixed-size Sign-magnitude This representation of negative numbers is called the Ten’s complement (i.e. the base is ten) EECS 1520 -- Computer Use: Fundamentals

9 Number systems: Two’s Complement 9 From the formula: To represent negative numbers using binary numbers, we use “Two’s Complement” (i.e. base is 2) Example: how do we represent “- 5 ” using 4 digits (or bits) with “Two’s Complement” (11) 10 in binary number is: 1011 So - 5 in base 10 is represented as 1011 using 4-bit binary numbers EECS 1520 -- Computer Use: Fundamentals

10 Number systems: Two’s Complement 10 Example: what about “-1” using 4 digits (or bits) with “Two’s Complement” (15) 10 in binary number is: 1111 So - 1 in base 10 is represented as 1111 using 4-bit binary numbers These are called “Signed binary numbers using two’s complement” If the leftmost bit is “0”, the number is positive If the leftmost bit is “1”, the number is negative EECS 1520 -- Computer Use: Fundamentals

11 Number systems: Two’s Complement 11 How do we find the two’s complement binary numbers of a negative integer? Method: 1) Start with the positive version of the number 2) invert all the bits (i.e. 10, and 0 1) 3) Add 1 Back to the first example: what is the two’s complement binary number of “-5” in 4-bit? Step 1) look for the 4- bit binary number for +5, which is 0101 Step 2) invert all the bits, so 0101 now becomes 1010 Step 3) Add 1, so 1010 becomes 1011 EECS 1520 -- Computer Use: Fundamentals

12 Number systems: Two’s Complement 12 Natural number4-bit binary2’s complement 000000 100011 200102 300113 401004 501015 601106 701117 81000-8 91001-7 101010-6 111011-5 121100-4 131101-3 141110-2 151111 EECS 1520 -- Computer Use: Fundamentals

13 Number systems: Two’s Complement 13 Examples of addition and subtraction Calculate the following in 4-bit two’s complement representation. Step 1: convert -7 and 3 to binary numbers.Answer: 7 in 4 bits representation is: 0111 -7 in 4 bits two’s complement representation is: 1001 Step 2: EECS 1520 -- Computer Use: Fundamentals

14 Number systems: Two’s Complement 14 Examples of addition and subtraction Calculate the following in 5-bit two’s complement representation. EECS 1520 -- Computer Use: Fundamentals

15 Number systems: Two’s Complement 15 Step 1: convert -4 and -5 to binary numbers. Answer: 4 in 5-bit representation is: 00100 5 in 5-bit representation is: 00101 Step 2: 11100 11011 Invert and add 1 01001 = (9) 10 EECS 1520 -- Computer Use: Fundamentals

16 16 Sample Test/Exam Question: Number systems: Two’s Complement Convert -173 to 12-bit two’s complement representation. Show all your work Step 1: convert 173 to binary by repeated division by 2.Answer: 173/2861 86/2430 43/2211 21/2101 10/250 5/221 2/210 1/2 01 Step 2: expand answer in Step 1 to 12-bits. 10101101 000010101101 Step 3: invert the bits and add one 111101010010 + 1 111101010011 -173 in 12-bit is: 111101010011 EECS 1520 -- Computer Use: Fundamentals

17 17 Sample Test/Exam Question: Number systems: Two’s Complement Convert the 8-bit two’s complement under 11001100 to decimal. Answer: -52 EECS 1520 -- Computer Use: Fundamentals

18 18 Unsigned VS Signed Two’s Complement EECS 1520 -- Computer Use: Fundamentals In Ch. 2, we learned how to perform subtraction between 2 unsigned binary numbers Example: Compute the following by 2 methods: 1) using 5-bit unsigned binary numbers 2) using 5-bit two’s complement Both methods will give the same answer!

19 19 Analog VS Digital analog data: information represented in a continuous form digital data: information represented in a discrete form Advantages of digital data: 1.A binary digit is 0 or 1, which can be represented by high and low state in an electronic signal 2.Electronic signals fluctuate – digital signal is far more resistant to information loss because of distance between the two states Continuous formDiscrete form EECS 1520 -- Computer Use: Fundamentals


Download ppt "1 Week 3: Data Representation: Negative Numbers READING: Chapter 3."

Similar presentations


Ads by Google