Data Representation – Chapter 3 Sections 3-2, 3-3, 3-4.
-
Upload
jemimah-rice -
Category
Documents
-
view
232 -
download
1
Transcript of Data Representation – Chapter 3 Sections 3-2, 3-3, 3-4.
![Page 1: Data Representation – Chapter 3 Sections 3-2, 3-3, 3-4.](https://reader035.fdocuments.net/reader035/viewer/2022081501/56649da95503460f94a975c9/html5/thumbnails/1.jpg)
Data Representation – Chapter 3
Sections 3-2, 3-3, 3-4
![Page 2: Data Representation – Chapter 3 Sections 3-2, 3-3, 3-4.](https://reader035.fdocuments.net/reader035/viewer/2022081501/56649da95503460f94a975c9/html5/thumbnails/2.jpg)
Homework Assignment
• Questions/Requests/Comments/Concerns?
![Page 3: Data Representation – Chapter 3 Sections 3-2, 3-3, 3-4.](https://reader035.fdocuments.net/reader035/viewer/2022081501/56649da95503460f94a975c9/html5/thumbnails/3.jpg)
Number Range
• What is meant by “range”?
• How do you compute the range of a given set of digits?
![Page 4: Data Representation – Chapter 3 Sections 3-2, 3-3, 3-4.](https://reader035.fdocuments.net/reader035/viewer/2022081501/56649da95503460f94a975c9/html5/thumbnails/4.jpg)
More Representations
• Integer values are represented by a power series regardless of radix (base)1410 = 1 x 101 + 4 x 100
11102 = 1 x 23 + 1 x 22 + 1 x 21 + 0 x 20
• What about fractions?
![Page 5: Data Representation – Chapter 3 Sections 3-2, 3-3, 3-4.](https://reader035.fdocuments.net/reader035/viewer/2022081501/56649da95503460f94a975c9/html5/thumbnails/5.jpg)
Fraction Representations• Same principle applies
14.3710 = 1 x 101 + 4 x 100 + 3 x 10-1 + 7 x 10-2
“decimal point”
10.112 = 1 x 21 + 0 x 20 + 1 x 2-1 + 1 x 2-2
“binary point”
![Page 6: Data Representation – Chapter 3 Sections 3-2, 3-3, 3-4.](https://reader035.fdocuments.net/reader035/viewer/2022081501/56649da95503460f94a975c9/html5/thumbnails/6.jpg)
Fraction Representations• To convert a decimal fraction to binary
– Multiple decimal fractional part by 2– Output the integer part– Repeat until you’ve reached the desired accuracy
0.312510 = 0.01012
.3125 x 20.6250
.6250 x 21.2500
.2500 x 20.5000
.5000 x 21.0000
![Page 7: Data Representation – Chapter 3 Sections 3-2, 3-3, 3-4.](https://reader035.fdocuments.net/reader035/viewer/2022081501/56649da95503460f94a975c9/html5/thumbnails/7.jpg)
Fraction Representations• More on binary floating point
representations later (maybe)
![Page 8: Data Representation – Chapter 3 Sections 3-2, 3-3, 3-4.](https://reader035.fdocuments.net/reader035/viewer/2022081501/56649da95503460f94a975c9/html5/thumbnails/8.jpg)
Addition
• Decimal number addition
carry121
+19
40
![Page 9: Data Representation – Chapter 3 Sections 3-2, 3-3, 3-4.](https://reader035.fdocuments.net/reader035/viewer/2022081501/56649da95503460f94a975c9/html5/thumbnails/9.jpg)
Addition
• Binary number addition
1 111 carries
10101
+10011
101000• It works the same way in binary as it
does in decimal
![Page 10: Data Representation – Chapter 3 Sections 3-2, 3-3, 3-4.](https://reader035.fdocuments.net/reader035/viewer/2022081501/56649da95503460f94a975c9/html5/thumbnails/10.jpg)
Subtraction
• Decimal number subtraction
1 1 borrow
21
+19
02
![Page 11: Data Representation – Chapter 3 Sections 3-2, 3-3, 3-4.](https://reader035.fdocuments.net/reader035/viewer/2022081501/56649da95503460f94a975c9/html5/thumbnails/11.jpg)
Subtraction
• Binary number subtraction
01 borrow
10101
-10011
00010• It works the same way in binary as it
does in decimal
![Page 12: Data Representation – Chapter 3 Sections 3-2, 3-3, 3-4.](https://reader035.fdocuments.net/reader035/viewer/2022081501/56649da95503460f94a975c9/html5/thumbnails/12.jpg)
Subtraction Questions
• What about when a subtraction results in a negative value?
• How do you represent a negative value in binary?
• How do you represent a negative value in decimal?
![Page 13: Data Representation – Chapter 3 Sections 3-2, 3-3, 3-4.](https://reader035.fdocuments.net/reader035/viewer/2022081501/56649da95503460f94a975c9/html5/thumbnails/13.jpg)
Negative Numbers
• Decimal
- 27
• Place a “-” in front of the decimal digits
![Page 14: Data Representation – Chapter 3 Sections 3-2, 3-3, 3-4.](https://reader035.fdocuments.net/reader035/viewer/2022081501/56649da95503460f94a975c9/html5/thumbnails/14.jpg)
Negative Numbers
• Binary
-11011
• Place a “-” in front of the binary digits
• This is called signed-magnitude representation
• In the hardware the sign is a designated bit where 0 means “+” and 1 mean “–”
![Page 15: Data Representation – Chapter 3 Sections 3-2, 3-3, 3-4.](https://reader035.fdocuments.net/reader035/viewer/2022081501/56649da95503460f94a975c9/html5/thumbnails/15.jpg)
Negative Numbers
• Signed-magnitude is convenient for humans doing symbolic manipulations
• It’s not convenient for computer computations
• Why not?– Consider subtraction (which is really
addition of a negative number)
![Page 16: Data Representation – Chapter 3 Sections 3-2, 3-3, 3-4.](https://reader035.fdocuments.net/reader035/viewer/2022081501/56649da95503460f94a975c9/html5/thumbnails/16.jpg)
Signed-Magnitude Subtraction
21 Minuend
-17 Subtrahend
4 Difference
17 Minuend
-21 Subtrahend
- 4 Difference
![Page 17: Data Representation – Chapter 3 Sections 3-2, 3-3, 3-4.](https://reader035.fdocuments.net/reader035/viewer/2022081501/56649da95503460f94a975c9/html5/thumbnails/17.jpg)
Signed-Magnitude Subtraction
if (M >= S)compute M-S
elsecompute S-Mplace “-” on difference
• To perform subtraction we must first perform a comparison!– Therein lies the inconvenience
![Page 18: Data Representation – Chapter 3 Sections 3-2, 3-3, 3-4.](https://reader035.fdocuments.net/reader035/viewer/2022081501/56649da95503460f94a975c9/html5/thumbnails/18.jpg)
Negative Numbers Revisited• Complement representation
• “(r-1)’s” complement– Given Nr , an n-digit number of radix r
– “(r-1)’s” complement is (rn – 1) - Nr
![Page 19: Data Representation – Chapter 3 Sections 3-2, 3-3, 3-4.](https://reader035.fdocuments.net/reader035/viewer/2022081501/56649da95503460f94a975c9/html5/thumbnails/19.jpg)
“(r-1)’s” complement
• Decimal1234510 n=5, r=10, N=12345
(105-1)-12345
(100000-1)-12345
99999-12345
87654– “9’s” complement
![Page 20: Data Representation – Chapter 3 Sections 3-2, 3-3, 3-4.](https://reader035.fdocuments.net/reader035/viewer/2022081501/56649da95503460f94a975c9/html5/thumbnails/20.jpg)
“(r-1)’s” complement
• Binary101102 n=5, r=2, N=10110(25-1)-10110(100000-1)-1011011111-1011001001
– “1’s” complement– Note that the result is just an inversion of
the bits of the original number (1/0 and 0/1)
![Page 21: Data Representation – Chapter 3 Sections 3-2, 3-3, 3-4.](https://reader035.fdocuments.net/reader035/viewer/2022081501/56649da95503460f94a975c9/html5/thumbnails/21.jpg)
“(r-1)’s” complement
• So, how does this help us do subtraction?– It doesn’t really!– Furthermore the representation of 0 is
ambiguous
0000 “+0”
1111 “-0”
Try it!
![Page 22: Data Representation – Chapter 3 Sections 3-2, 3-3, 3-4.](https://reader035.fdocuments.net/reader035/viewer/2022081501/56649da95503460f94a975c9/html5/thumbnails/22.jpg)
Another Complement
• “r’s” complement– Given Nr , an n-digit number of radix r
– “r’s” complement is “(r-1)’s” complement + 1
(rn – 1) – Nr+ 1 rn – Nr if (Nr != 0)
0 if (Nr == 0)
![Page 23: Data Representation – Chapter 3 Sections 3-2, 3-3, 3-4.](https://reader035.fdocuments.net/reader035/viewer/2022081501/56649da95503460f94a975c9/html5/thumbnails/23.jpg)
“r’s” complement
• Let’s concentrate on the 2’s complement (binary number system)– “…leaving all least significant 0’s and the
first 1 unchanged, and then replacing 1’s by 0’s and 0’s by 1’s in all other higher significant bits.”
• Yeah, right!– Take the “1’s” complement and add 1
![Page 24: Data Representation – Chapter 3 Sections 3-2, 3-3, 3-4.](https://reader035.fdocuments.net/reader035/viewer/2022081501/56649da95503460f94a975c9/html5/thumbnails/24.jpg)
“2’s” Complement
101102 n=5, r=2, N=10110
(25-1)-10110
(100000-1)-10110
11111-10110
01001 1’s complement
+ 1
01010 2’s complement
![Page 25: Data Representation – Chapter 3 Sections 3-2, 3-3, 3-4.](https://reader035.fdocuments.net/reader035/viewer/2022081501/56649da95503460f94a975c9/html5/thumbnails/25.jpg)
2’s Complement “Subtraction”
0111
-0101 0111
+1011
10010
Original Problem 2’s Complement Problem
Carry Out Answer2’s Complement
![Page 26: Data Representation – Chapter 3 Sections 3-2, 3-3, 3-4.](https://reader035.fdocuments.net/reader035/viewer/2022081501/56649da95503460f94a975c9/html5/thumbnails/26.jpg)
2’s Complement “Subtraction”
0101
-0111 0101
+1001
01110
Original Problem 2’s Complement Problem
Carry Out Answer2’s Complement
![Page 27: Data Representation – Chapter 3 Sections 3-2, 3-3, 3-4.](https://reader035.fdocuments.net/reader035/viewer/2022081501/56649da95503460f94a975c9/html5/thumbnails/27.jpg)
2’s Complement “Subtraction”
0101
+1001
01110
2’s Complement Problem
Carry Out Answer2’s Complement
0111
+1011
10010
2’s Complement Problem
Carry Out Answer2’s Complement
• How do we know the sign of the result?
![Page 28: Data Representation – Chapter 3 Sections 3-2, 3-3, 3-4.](https://reader035.fdocuments.net/reader035/viewer/2022081501/56649da95503460f94a975c9/html5/thumbnails/28.jpg)
2’s Complement “Subtraction”• Recall that we’re doing “fixed bit-length”
math
• When the numbers are “signed” then the most significant bit (MSB) represents the sign– MSB=1 defined as negative– MSB=0 defined as positive
![Page 29: Data Representation – Chapter 3 Sections 3-2, 3-3, 3-4.](https://reader035.fdocuments.net/reader035/viewer/2022081501/56649da95503460f94a975c9/html5/thumbnails/29.jpg)
Sign of the Result?
0101
+1001
01110
2’s Complement Problem
Carry Out Answer2’s Complement
0111
+1011
10010
2’s Complement Problem
Carry Out Answer2’s Complement
Sign Bit Sign Bit
![Page 30: Data Representation – Chapter 3 Sections 3-2, 3-3, 3-4.](https://reader035.fdocuments.net/reader035/viewer/2022081501/56649da95503460f94a975c9/html5/thumbnails/30.jpg)
Sign of the Result?
• The result is in 2’s complement form– If the sign bit is 1, take the 2’s complement
of the result to read off the magnitude in “normal” binary notation
– If the sign bit is 0, the result is in “normal” binary notation
![Page 31: Data Representation – Chapter 3 Sections 3-2, 3-3, 3-4.](https://reader035.fdocuments.net/reader035/viewer/2022081501/56649da95503460f94a975c9/html5/thumbnails/31.jpg)
Overflow
• What if the result is too big?– Remember, this is fixed bit-length math– When you add to n-bit numbers the result
may be up to n+1 bits long
![Page 32: Data Representation – Chapter 3 Sections 3-2, 3-3, 3-4.](https://reader035.fdocuments.net/reader035/viewer/2022081501/56649da95503460f94a975c9/html5/thumbnails/32.jpg)
Overflow
• When a signed positive number is added to a signed (2’s complement) negative number an overflow cannot occur– The positive number magnitude will only get smaller
• When two signed negative numbers or two positive numbers are added an overflow may occur– Magnitude is going to get bigger
• When two signed positive numbers are added, the sign bit is treated as part of the magnitude of the number and the end carry (overflow) is not treated as “overflow”
![Page 33: Data Representation – Chapter 3 Sections 3-2, 3-3, 3-4.](https://reader035.fdocuments.net/reader035/viewer/2022081501/56649da95503460f94a975c9/html5/thumbnails/33.jpg)
Overflow
• So, when is that “extra bit” an overflow and when is it part of the result?– Check the sign bit and the overflow bit– If the two are not equal then an overflow
has occurred– If the two are equal then there is no
overflow
![Page 34: Data Representation – Chapter 3 Sections 3-2, 3-3, 3-4.](https://reader035.fdocuments.net/reader035/viewer/2022081501/56649da95503460f94a975c9/html5/thumbnails/34.jpg)
Overflow
• In an overflow condition– If the numbers are both positive, then the
sign bit becomes part of the magnitude of the result
– If the numbers are both negative, then the overflow bit is treated as the sign
• As we’ll see next time (logic gates) there is a very simple way to detect overflow
• Questions?
![Page 35: Data Representation – Chapter 3 Sections 3-2, 3-3, 3-4.](https://reader035.fdocuments.net/reader035/viewer/2022081501/56649da95503460f94a975c9/html5/thumbnails/35.jpg)
“2’s” Complement
• Is this cheating on the representation of 0 since we handled it with an conditional statement?
• No! Try it based on the formula only!
• Don’t really need to specify it as two cases
• I only did it because the book did
![Page 36: Data Representation – Chapter 3 Sections 3-2, 3-3, 3-4.](https://reader035.fdocuments.net/reader035/viewer/2022081501/56649da95503460f94a975c9/html5/thumbnails/36.jpg)
“2’s” Complement• So, how does this help us do subtraction?• Addition of a 2’s complement subtrahend.
– Take 2’s complement of the subtrahend (negate the subtrahend)
– Add minuend to subtrahend– Difference is in 2’s complement notation
• Note we’re using a fixed, pre-specified number of bits
• If the result overflows, just ignore it for now
![Page 37: Data Representation – Chapter 3 Sections 3-2, 3-3, 3-4.](https://reader035.fdocuments.net/reader035/viewer/2022081501/56649da95503460f94a975c9/html5/thumbnails/37.jpg)
Other Binary Formats• Binary Coded Decimal (BCD)
– Convert each decimal digit to its binary representation and store– Requires 4 bits per digit – why?– Forget about BCD arithmetic
• Even parity/Odd parity– For a binary number, count the number of 1’s– If the number is odd, attach an extra “1” for even parity or a “0” for odd parity– If the number is even, attach an extra “0” for even parity or a “1” for odd parity– What good is this?
• Gray code– Count through a sequence of binary numbers in such a way that only 1 bit at a time
changes– What good is this?
• American Standard Code for Information Interchange (ASCII)– Standard set of binary patterns assigned to 256 characters– How many bits comprise the ASCII code?
• Unicode– Up-to-date replacement for ASCII– Various schemes utilizing various bit lengths
![Page 38: Data Representation – Chapter 3 Sections 3-2, 3-3, 3-4.](https://reader035.fdocuments.net/reader035/viewer/2022081501/56649da95503460f94a975c9/html5/thumbnails/38.jpg)
Homework
• Textbook pages 89 – 91– 3-9, 3-10, 3-13, 3-15, 3-16, 3-17, 3-23
– Due beginning of next lecture
• Begin reading Chapter 1