1 Week 3: Data Representation: Negative Numbers READING: Chapter 3.
-
Upload
milton-henry-simmons -
Category
Documents
-
view
231 -
download
2
Transcript of 1 Week 3: Data Representation: Negative Numbers READING: Chapter 3.
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
Number systems
3
•Sign-Magnitude Representation in base 10:Examples : + 5 - 6
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.
• How do we represent negative numbers in a computer system?
EECS 1520 -- Computer Use: Fundamentals
Number systems
4
0 1 2 3
1
…... 49
2 3 49
-1-2-3-49-50
0
…...
98975150 …... …...99 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
Number systems
5
5- 2+
598+
103 3Signed-magnitude Fixed-size
- 46+
2
966+
102Signed-magnitude Fixed-size
• Examples:
0 1 2 3
1
…... 49
2 3 49
-1-2-3-49-50
0
…...
98975150 …... …...99 Fixed-size
Sign-magnitude
Note: “1” is the carrier and is discarded
EECS 1520 -- Computer Use: Fundamentals
Number systems
6
• So far we used the number line to calculate the negative representation of a number.
0 1 2 3
1
…... 49
2 3 49
-1-2-3-49-50
0
…...
98975150 …... …...99 Fixed-size
Sign-magnitude
• In general, a formula that computes the negative representation is:
IBI k B = Base; I = integer; k = number of digits
982102 2
• From the above number line, to calculate the negative representation for -2:
• Example:
EECS 1520 -- Computer Use: Fundamentals
Number systems
7
• Apply the same formula: 964104 2 955105 2 946106 2
50501050 2
…... 937107 2
• How about “-4”, “-5”, “-6” … “-50”?
0 1 2 3
1
…... 49
2 3 49
-1-2-3-49-50
0
…...
98975150 …... …...99 Fixed-size
Sign-magnitude
EECS 1520 -- Computer Use: Fundamentals
Number systems
8
0 1 2 3
1
…... 49
2 3 49
-1-2-3-49-50
0
…...
98975150 …... …...99 Fixed-size
Sign-magnitude
• This representation of negative numbers is called the Ten’s complement (i.e. the base is ten)
II k 10
EECS 1520 -- Computer Use: Fundamentals
Number systems: Two’s Complement
9
• From the formula: II k 2
• 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”
11525 4
• (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
10
15121 4
• 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
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. 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
12
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
EECS 1520 -- Computer Use: Fundamentals
Number systems: Two’s Complement
13
• Examples of addition and subtraction
Calculate the following in 4-bit two’s complement representation.
-73+
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
10010011+
Step 2:
1100
EECS 1520 -- Computer Use: Fundamentals
Number systems: Two’s Complement
14
• Examples of addition and subtraction
Calculate the following in 5-bit two’s complement representation.
- 45-
EECS 1520 -- Computer Use: Fundamentals
Number systems: Two’s Complement
15
- 4-5+
Step 1: convert -4 and -5 to binary numbers.
Answer:
4 in 5-bit representation is: 00100 5 in 5-bit representation is: 00101
1110011011+
Step 2:
1 10111
11100
11011
Invert and add 1
Invert and add 1
Invert and add 101001 = (9)10
EECS 1520 -- Computer Use: Fundamentals
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/2 86 186/2 43 043/2 21 121/2 10 110/2 5 05/2 2 12/2 1 01/2 0 1
Step 2: expand answer in Step 1 to 12-bits.
10101101
000010101101
Step 3: invert the bits and add one 111101010010+ 1111101010011-173 in 12-bit is: 111101010011
EECS 1520 -- Computer Use: Fundamentals
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
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 numbers2) using 5-bit two’s complement
13- 6
• Both methods will give the same answer!
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
x(t)
tContinuous form Discrete form
x(n)
n
.... . . . ... . . . . . ..
. . . . . . .
5V
0V
1 0 1
EECS 1520 -- Computer Use: Fundamentals