200 likes | 220 Views
Week 3: Data Representation: Negative Numbers READING: Chapter 3. 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. EECS 1520 -- Computer Use: Fundamentals.
E N D
Week 3:Data Representation: Negative Numbers • READING: Chapter 3
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
EECS 1520 -- Computer Use: Fundamentals Number systems • Sign-Magnitude Representation in base 10: Examples : + 5 - 6 • How do we represent negative numbers in a computer system? Sign indicates positive Magnitude is 5 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.
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.
EECS 1520 -- Computer Use: Fundamentals Number systems Fixed-size • Examples: Sign-magnitude Note: “1” is the carrier and is discarded Signed-magnitude Fixed-size Signed-magnitude Fixed-size
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:
EECS 1520 -- Computer Use: Fundamentals Number systems • How about “-4”, “-5”, “-6” … “-50”? • Apply the same formula: Fixed-size Sign-magnitude
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
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
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
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
EECS 1520 -- Computer Use: Fundamentals Number systems: Two’s Complement
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:
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.
EECS 1520 -- Computer Use: Fundamentals Number systems: Two’s Complement Answer: Step 1: convert -4 and -5 to binary numbers. Invert and add 1 11100 4 in 5-bit representation is: 00100 5 in 5-bit representation is: 00101 Invert and add 1 11011 Step 2: Invert and add 1 01001 = (9)10
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 1 86/2 43 0 43/2 21 1 21/2 10 1 10/2 5 0 5/2 2 1 2/2 1 0 1/2 0 1 10101101 000010101101 Step 2: expand answer in Step 1 to 12-bits. Step 3: invert the bits and add one 111101010010 + 1 111101010011 -173 in 12-bit is: 111101010011
EECS 1520 -- Computer Use: Fundamentals Number systems: Two’s Complement Sample Test/Exam Question: Convert the 8-bittwo’s complement under 11001100 to decimal. Answer: -52
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!
EECS 1520 -- Computer Use: Fundamentals Analog VS Digital • analog data: information represented in a continuous form • digital data: information represented in a discrete form • Advantages of digital data: Continuous form Discrete form • A binary digit is 0 or 1, which can be represented by high and low state in an electronic signal • Electronic signals fluctuate – digital signal is far more resistant to information loss because of distance between the two states