unit: data and representation goal: learn how 0,1 can be used to represent numbers, text, images,...
TRANSCRIPT
UNIT: Data and UNIT: Data and RepresentationRepresentation
Goal: Learn how 0,1 can be used to Goal: Learn how 0,1 can be used to represent numbers, text, images, represent numbers, text, images, sound.sound.– Facility with binary numbersFacility with binary numbers– Understand methods of text compressionUnderstand methods of text compression– Understand concepts such as Understand concepts such as
discretization/digitization, bit depth, discretization/digitization, bit depth, resolution, sampling, in the context of resolution, sampling, in the context of images and sound.images and sound.
Representations are Representations are Arbitrary!Arbitrary!
'When 'When II use a word,' Humpty use a word,' Humpty Dumpty said, in a rather Dumpty said, in a rather scornful tone,' it means just scornful tone,' it means just what I choose it to mean, what I choose it to mean, neither more nor less.‘neither more nor less.‘
'The question is,' said Alice, 'The question is,' said Alice, 'whether you 'whether you cancan make make words mean so many words mean so many different things.different things.
''The question is,' said Humpty ''The question is,' said Humpty Dumpty, 'which is to be Dumpty, 'which is to be master - that's all.'master - that's all.'
Symbols & RepresentationSymbols & Representation
““analogous”analogous”
Symbols & RepresentationSymbols & Representation
““abstract”abstract”
aattggaagcaaatgacatcacagcaggtcagag…
DigitalDigital
““Relating to, or resembling a digit”Relating to, or resembling a digit” ““Expressed in discrete numerical Expressed in discrete numerical
form, especially for use by a form, especially for use by a computer or other electronic device”computer or other electronic device”
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23...
Analog or Digital?Analog or Digital?
Number RepresentationsNumber Representations
1/2 + 1/6 + 1/9
5137
011000101000
Analog Digital
are analogous not necessarily analogous
can be continuous discrete symbols
precision a problem add extra digits
errors hard to detect errors easier to detect
requires little decoding requires some decoding
Why Binary?Why Binary?
Because 2 is special! It’s the smallest Because 2 is special! It’s the smallest number that’s not 1.number that’s not 1.
So simple a computer could do it!So simple a computer could do it!
Why Binary?Why Binary?
So simple a computer could do it!So simple a computer could do it!
Bits, Bytes, WordsBits, Bytes, Words
Bit = 0 or 1. One character of informationBit = 0 or 1. One character of information Byte = 8 bits. Example: 01101101Byte = 8 bits. Example: 01101101 Word = some number of bytes, depending Word = some number of bytes, depending
on computer. on computer. Typical = 4 bytes = 32 bitsTypical = 4 bytes = 32 bits
““A 32-bit machine”A 32-bit machine”
Word size limits what can be represented Word size limits what can be represented easilyeasily
(more on this later)(more on this later)
51371
00
0
10
01
0 1
Decimal Numbers….
67895
0234
1 01
decimal digits binary digits
Binary Numbers….
b its
bits
16
8 4 2 1
0 1 1 1116 + 4 + 2 + 1 =
23
16
8 4 2 1
1 1 1 008 + 4 + 2 = 14
16
8 4 2 1
1 1 0 1116 + 8 + 4 + 1 =
29
1 1 0 1 8 + 4 + 1 = 13
16
8 4 2 1
16
8 2 1
There are 10 kinds of people in the world:
-Those who know binary-Those who do not
Other Bases
Base 3: 2012 = 2 x 33 + 0 x 32 + 1 x 31 + 2 x 30
= 2 x 27 + 0 x 9 + 1 x 3 + 2 x 1
= 54 + 0 + 3 + 2
= 59
Other BasesBase 16…. what are the numerals ?
Base 2: {0,1}Base 3: {0,1,2}Base 10: {0,1,2,3,4,5,6,7,8,9}
Base 16: {0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F}
1CF == 1 x 162 + 12 x 161 + 15 x 160
= 256 + 192 + 15 = 463
Word sizeWord size
8 bits:8 bits:
can only represent 11111111 = 255can only represent 11111111 = 255
16 bits: 16 bits:
can represent 1111111111111111 = 65535can represent 1111111111111111 = 65535
32 bits:32 bits:
can represent 4292967295can represent 4292967295
Binary and HexadecimalBinary and Hexadecimal
How do we write 463 How do we write 463 in Binary?in Binary?
463=256 + 128 + 64463=256 + 128 + 64 + 8 + 4 + 2 + 1 + 8 + 4 + 2 + 1
463 = 111001111463 = 111001111 463 = 1 1100 1111463 = 1 1100 1111 463 = 1 C F in Hex463 = 1 C F in Hex
and vice-versa:and vice-versa: 2EA = 0010 1110 2EA = 0010 1110
10101010
00 00000000
11 00000011
22 00100100
33 00100111
44 01001000
55 01001011
66 01101100
77 01101111
88 10001000
99 10011001
A A (10)(10)
10101010
B B (11)(11)
10111011
C C (12)(12)
11001100
D D (13)(13)
11011101
E (14)E (14) 11101110
F (15)F (15) 11111111
Binary Addition
Decimal Addition
8423 6915----------
Binary Addition
1101011 1001101 ------------
see http://www.youtube.com/watch?v=GcDshWmhF4A
Binary Subtraction
What about 1001101 – 1101011?How do we represent negative numbers?
Decimal Subtraction
8423 6915----------
Binary Subtraction
1101011 1001101 ------------ WAIT!
Properties of a Properties of a Good RepresentationGood Representation
Should be simpleShould be simple
Allows us to do useful thingsAllows us to do useful things
Should be efficientShould be efficient
Recovering from errors should be Recovering from errors should be possiblepossible
Negative NumbersNegative Numbers
How would we represent -5 in binary?How would we represent -5 in binary?
Sign-magnitude: use an extra bit for +/-Sign-magnitude: use an extra bit for +/-+ 5 = (+) 101 = 00000101+ 5 = (+) 101 = 00000101- 5 = (-) 101 = 100001015 = (-) 101 = 10000101
(Assumes we have a fixed (known) word size (example above: 8) (Assumes we have a fixed (known) word size (example above: 8) so that we know the first bit represents +/-, not the next power of so that we know the first bit represents +/-, not the next power of 2)2)
What about 0?What about 0?– different 0’s can cause problemsdifferent 0’s can cause problems
Negative NumbersNegative Numbers
Sign-magnitude (use one bit for sign) is Sign-magnitude (use one bit for sign) is perfectly acceptable, with minor perfectly acceptable, with minor inconvenience, but is not as good as a inconvenience, but is not as good as a clever alternative.clever alternative.
““Two’s complement notation” is used. Two’s complement notation” is used. – only one representation of 0only one representation of 0– allows computation of subtraction via additionallows computation of subtraction via addition– a bit confusing on first sight.a bit confusing on first sight.
Towards understanding two’s Towards understanding two’s
complement:complement: Using 1 digit Using 1 digit
Suppose we had the symbols 0 Suppose we had the symbols 0 through 9. How many (positive and through 9. How many (positive and negative) numbers can we negative) numbers can we represent?represent?
Which symbol gets which number?Which symbol gets which number?
10’s complement10’s complement
representation
01
2
3
45
9
8
7
6
01
2
3
4-5
-1
-2
-3
-4
addition of positive numbers works fine?
OVERFLOW: if sum is more than max positive, it becomes negative: 2+4 = - 4(unavoidable since we have a fixed word size)
value represented
10’s complement10’s complement
representation
01
2
3
45
9
8
7
6
01
2
3
4-5
-1
-2
-3
-4
addition of positive and negative numbers…
ex: 4-2 = 4 + (-2) represented by 4+8 = 12and 122 on this clock.…works because going clockwise 8 is like going counterclockwise 2
value represented
10’s complement10’s complement
Addition is moving forward along the Addition is moving forward along the number line, subtraction is moving number line, subtraction is moving backback
A – B = A + (– B)A – B = A + (– B)
Clock DiagramClock Diagram
Using 3 digitsUsing 3 digits
If we had 3 digits, which positive and If we had 3 digits, which positive and negative numbers could we negative numbers could we represent?represent?
What would the representation of -7 What would the representation of -7 be?be?
1000’s clock1000’s clock
10’s complement10’s complement
0100
200
300
400500
900
800
700
600
000100
200
300
400-500
-100
-200
-300
-400
addition of positive and negative numbers…
…works because going clockwise n is like going counterclockwise 1000-n
representation
value represented
Moving to BinaryMoving to Binary
Using 4 bits, which positive and negative Using 4 bits, which positive and negative numbers can we represent?numbers can we represent?
What is the representation of -5?What is the representation of -5?
Given a binary number, how can we quickly Given a binary number, how can we quickly find out what number it represents?find out what number it represents?
2’s Complement2’s Complement
2’s complement2’s complement
0000
01
2
3
4-5
-1
-2
-3-4
1000
01001100
00010010
0011
0101
0110
01111001
1010
1011
1101
1110
1111
5-6
-7 -
8
6
7
java int: 32 bits: 231-1 = 2,147,483,647java long: 64 bits: 9,223,372,036,854,775,807
6-4 = 6 + -4 = 0110 + 1100 = 10010
= 0010(ignoring overflow bit)
Real NumbersReal Numbers
16 8 4 2 1
1/4 1/8 1/161/2
11 0 . 0 1 0 10 . 0 1 0 1
= 2 + 1/4 + 1/16 = 2 + 1/4 + 1/16
= 2 + .25 + .0625= 2 + .25 + .0625
= 2.3125= 2.3125
2 1 1/4 1/8 1/161/2
Recall Scientific NotationRecall Scientific Notation
3204.671 = 3.204671 x 103204.671 = 3.204671 x 1033
.0003671 = 3.671 x 10.0003671 = 3.671 x 10-4-4
MantissaMantissa ExponentExponent
Base 10 representation: (3.671, -4)Base 10 representation: (3.671, -4)
multiplying by 10 is just shifting by one position
Binary Scientific NotationBinary Scientific Notation
.001101 = 1.101 x 2.001101 = 1.101 x 2-3-3
1101.11 = 1.10111 x 21101.11 = 1.10111 x 233
MantissaMantissa Exponent Exponent Base 2 representation: (1.10111, 11)Base 2 representation: (1.10111, 11) Note, mantissa is always “1” (why?) Note, mantissa is always “1” (why?)
so can just store (10111, 11) so can just store (10111, 11) What if mantissa or exponent is What if mantissa or exponent is
negative?negative?
multiplying by 2 is just shifting by one position
SummarySummary
Binary notation uses numerals 0,1 Binary notation uses numerals 0,1 and place values that are powers of 2and place values that are powers of 2
Use “clock” to see how negative Use “clock” to see how negative numbers are represented in 2’s numbers are represented in 2’s complement form; subtracting can be complement form; subtracting can be done by adding.done by adding.
Real valued numbers are represented Real valued numbers are represented by a pair of integers: mantissa and by a pair of integers: mantissa and exponentexponent