Week 3 Data Representation Negative Numbers READING Chapter
Week 3: Data Representation: Negative Numbers • READING: Chapter 3 1
EECS 1520 -- Computer Use: Fundamentals Data representation How do we represent data in a computer system? • Numbers • Negative numbers • Text • Audio • Images and graphics • Video 2
EECS 1520 -- Computer Use: Fundamentals Number systems • How do we represent negative numbers in a computer system? • Sign-Magnitude Representation in base 10: Examples : + 5 Sign indicates positive Magnitude is 5 - 6 Sign indicates negative Magnitude is 6 • In Binary Systems, we can use the most significant bit to represent the “sign” Examples: 00101 is +5 10101 is -5 • The problem is: 00000 is + 0 10000 is - 0 • The two representations of zero within a computer can cause unnecessary complexity. 3
EECS 1520 -- Computer Use: Fundamentals Number systems • 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. Fixed-size Sign-magnitude • To perform addition, we just add the numbers together and discard any carry. 4
EECS 1520 -- Computer Use: Fundamentals Number systems Fixed-size • Examples: Sign-magnitude Note: “ 1” is the carrier and is discarded Signed-magnitude Fixed-size 5
EECS 1520 -- Computer Use: Fundamentals Number systems • 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: 6
EECS 1520 -- Computer Use: Fundamentals Number systems • How about “-4”, “-5”, “-6” … “-50”? • Apply the same formula: Fixed-size Sign-magnitude 7
EECS 1520 -- Computer Use: Fundamentals Number systems • This representation of negative numbers is called the Ten’s complement (i. e. the base is ten) Fixed-size Sign-magnitude 8
EECS 1520 -- Computer Use: Fundamentals Number systems: Two’s Complement • To represent negative numbers using binary numbers, we use “Two’s Complement” (i. e. base is 2) • From the formula: • 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 9
EECS 1520 -- Computer Use: Fundamentals Number systems: Two’s Complement • 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 10
EECS 1520 -- Computer Use: Fundamentals Number systems: Two’s Complement • 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. 1 0, 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 11
EECS 1520 -- Computer Use: Fundamentals Number systems: Two’s Complement Natural number 4 -bit binary 2’s complement 0 0000 0 1 0001 1 2 0010 2 3 0011 3 4 0100 4 5 0101 5 6 0110 6 7 0111 7 8 1000 -8 9 1001 -7 10 1010 -6 11 1011 -5 12 1100 -4 13 1101 -3 14 1110 -2 15 1111 -1 12
EECS 1520 -- Computer Use: Fundamentals Number systems: Two’s Complement • Examples of addition and subtraction Calculate the following in 4 -bit two’s complement representation. Answer: Step 1: convert -7 and 3 to binary numbers. 7 in 4 bits representation is: 0111 -7 in 4 bits two’s complement representation is: 1001 Step 2: 13
EECS 1520 -- Computer Use: Fundamentals Number systems: Two’s Complement • Examples of addition and subtraction Calculate the following in 5 -bit two’s complement representation. 14
EECS 1520 -- Computer Use: Fundamentals Number systems: Two’s Complement Answer: Step 1: convert -4 and -5 to binary numbers. 4 in 5 -bit representation is: 00100 5 in 5 -bit representation is: 00101 Invert and add 1 11100 11011 Step 2: Invert and add 1 01001 = (9)10 15
EECS 1520 -- Computer Use: Fundamentals Number systems: Two’s Complement Sample Test/Exam Question: Convert -173 to 12 -bit two’s complement representation. Show all your work Answer: Step 1: convert 173 to binary by repeated division by 2. 173/2 86/2 43/2 21/2 10/2 5/2 2/2 1/2 86 43 21 10 5 2 1 0 1 0 1 10101101 Step 2: expand answer in Step 1 to 12 -bits. 000010101101 Step 3: invert the bits and add one 111101010010 + 1 111101010011 -173 in 12 -bit is: 111101010011 16
EECS 1520 -- Computer Use: Fundamentals Number systems: Two’s Complement Sample Test/Exam Question: Convert the 8 -bit two’s complement under 1100 to decimal. Answer: -52 17
EECS 1520 -- Computer Use: Fundamentals Unsigned VS Signed Two’s Complement • 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! 18
EECS 1520 -- Computer Use: Fundamentals Analog VS Digital • analog data: information represented in a continuous form • digital data: information represented in a discrete form Continuous form 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 19
- Slides: 19