Download Lecture 2

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Location arithmetic wikipedia , lookup

Mathematics and architecture wikipedia , lookup

Addition wikipedia , lookup

Approximations of π wikipedia , lookup

Elementary mathematics wikipedia , lookup

Arithmetic wikipedia , lookup

Positional notation wikipedia , lookup

Transcript
COSC 243
Data Representation 2
COSC 243 (Computer Architecture)
Lecture 2 - Data Representation 2
1
Overview
• Last Lecture
– Data representation 1
• This Lecture
– Data representation 2
• Positional notation
• Positive integers
• Negative integers
– Source: lecture & lecture notes
• Next Lecture
– Data representation 3
– Source: lecture & lecture notes
COSC 243 (Computer Architecture)
Lecture 2 - Data Representation 2
2
Mathematical Notation
• Numeral / Digit - a single symbol representing a quantity
– In the case of the switches there are 2 (0 and 1)
– In the case of “normal math” there are 10 (0,1,2,3,4,5,6,7,8,9)
• Number system - a way of assigning combinations of
numerals to different quantities
– The Roman system is different from the Hindu–Arabic system
• MCMXIII = 1913
• Positional number system – a way of assigning
quantities to numerals based on their position
– 1981 = 1 lot of a thousand, 9 of a hundred, 8 of ten and 1 of 1
COSC 243 (Computer Architecture)
Lecture 2 - Data Representation 2
3
Number Systems
• In a positional system the base is the number of numerals
• Base 10 (decimal) is used today by most cultures. It
originated in the 13th century BC in China.
– Its easy because we have 10 fingers
COSC 243 (Computer Architecture)
Lecture 2 - Data Representation 2
4
Number Systems
• The Mayas, Celts and Aztecs developed a base 20
(vigesimal ) number system
http://en.wikipedia.org/wiki/Vigesimal
COSC 243 (Computer Architecture)
Lecture 2 - Data Representation 2
5
Number Systems
• The Sumerians and Babylonians developed a base 60
number system (sexagesimal)
http://en.wikipedia.org/wiki/Sexagesimal
COSC 243 (Computer Architecture)
Lecture 2 - Data Representation 2
6
Number Systems
• Computers work in base 2 (binary)
COSC 243 (Computer Architecture)
Lecture 2 - Data Representation 2
7
Switches
• Computer memory is made from (electronic) switches
• Each switch can be either: off or on
– If the switch is off then electricity does not flow
• We call this false or 0
– If the switch is on the electricity does flow
• We call this true or 1
or
OFF
COSC 243 (Computer Architecture)
ON
Lecture 2 - Data Representation 2
8
A Single Switch
• How many numbers can be represented with a single switch?
=0
=1
COSC 243 (Computer Architecture)
Lecture 2 - Data Representation 2
9
Two Switches
• How many numbers can be represented with two switches?
= 00
= 01
= 10
= 11
COSC 243 (Computer Architecture)
Lecture 2 - Data Representation 2
10
Base 10 Numbers
• The base 10 system has 10 symbols (numerals / digits):
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
• Each digit in the number represents a power of 10
– Working from the ‘.’
• Left increases +ve powers of 10
• Right decreases –ve powers of 10
• 123.45
= 1*102 + 2*101 + 3*100 + 4*10-1 + 5*10-2
= 100 + 20 + 3 + 4/10 + 5/100
COSC 243 (Computer Architecture)
Lecture 2 - Data Representation 2
11
Bases (The General Case)
• The general case is:
def.xyzb = d*b2 + e*b1 + f*b0 + x*b-1 + y*b-2 +z*b-3
• Where b is the base (specified as a subscript)
• If the base is less than or equal to 10 we (by convention)
use the Hindu–Arabic digits, if it is greater than 10 we use
alphabetical (Roman) letters. What if the base is larger
than 36?
COSC 243 (Computer Architecture)
Lecture 2 - Data Representation 2
12
Bases
• What is the value of:
13460 = 1*602 + 3*601 + 4*600 (sexagesimal)
13420 = 1*202 + 3*201 + 4*200 (vigesimal)
• What is the value of:
002 =
012 =
102 =
112 =
COSC 243 (Computer Architecture)
Lecture 2 - Data Representation 2
13
Binary
• What is the value of:
002 =
012 =
102 =
112 =
= 00
= 01
• When we see two switches together
we can interpret them as a number in
binary
COSC 243 (Computer Architecture)
Lecture 2 - Data Representation 2
= 10
= 11
14
Binary Number System
• So we can use the computer’s memory (the switchbox) to
represent numbers in binary (base 2).
• What is the value of:
– on on off off on on?
– 1100112
• 1*25 + 1*24 + 0*23 + 0*22 + 1*21 + 1*20
• 32 + 16 + 0 + 0
+2 +1
• 51
– on on off “point” off on on
– 110.0112
• 1*22 + 1*21 + 0*20 + 0*2-1 + 1*2-2 + 1*2-3
• 4 + 2 + 0 + 0 + 1/4 + 1/8
• 6.375
COSC 243 (Computer Architecture)
Lecture 2 - Data Representation 2
15
Decimal to Binary Conversion
• Convert 38.687510 to binary (base 2)
– We’ll do the 38 first then the .6875 second
• Repeatedly divide the whole number part by 2
– The remainders are the answer (bottom to top)
• From the next slide: 3810 = 1001102
COSC 243 (Computer Architecture)
Lecture 2 - Data Representation 2
16
Decimal to Binary Conversion (cont)
LSB = least significant bit MSB = most significant bit
COSC 243 (Computer Architecture)
Lecture 2 - Data Representation 2
17
Decimal to Binary Conversion (cont)
• Convert 38.687510 to binary (base 2)
• For the fractional part, multiply by 2. Split the result into
an integer part and a fractional part. Continue multiplying
the fractional part. Use the integer part from top to
bottom.
• From the next slide: .687510 = .10112
• So, 38.6875 = 100110.10112
COSC 243 (Computer Architecture)
Lecture 2 - Data Representation 2
18
Decimal to Binary Conversion (cont)
COSC 243 (Computer Architecture)
Lecture 2 - Data Representation 2
19
Repeating & Irrational Numbers
• When you turn a base 10 fraction into a decimal it may not
terminate, the result may repeat “forever”: 1/3 = 0.33333...
• What is the decimal equivalent of:
1/10
• What is the binary equivalent?
• In order to store the number we terminate significance at
some arbitrary point (often specified by the programmer
when they declare a variable (float / double in ‘C’)).
•
p (pi) is an irrational number. It cannot be represented
as a fraction (in base 10). The approximation of
3.141592… never terminates and never repeats
COSC 243 (Computer Architecture)
Lecture 2 - Data Representation 2
20
Binary to Decimal Conversion (cont)
• Working outward from the decimal point:
100110.1011
= 32 + 0 + 0 + 4 + 2 + 0 + 1/2 + 0 + 1/8 + 1/16
= 32 + 0 + 0 + 4 + 2 + 0 + (0.5 + 0 + 0.125 + 0.0625)
= 38.6875
COSC 243 (Computer Architecture)
Lecture 2 - Data Representation 2
21
Binary to Decimal Conversion Table
Power (bit)
2Power
0
1
1
2
2
4
3
8
4
16
5
32
6
64
7
128
8
256
9
512
10
1,024
11
2,048
12
4,096
13
8,192
14
16,384
15
32,768
16
65,536
17
131,072
18
262,144
19
524,288
20
1,048,576
COSC 243 (Computer Architecture)
Power (bit)
-1
-2
-3
-4
-5
-6
-7
-8
Lecture 2 - Data Representation 2
2Power
0.5
0.25
0.125
0.0625
0.03125
0.015625
0.007813
0.003906
22
Octal Number System
• Binary numbers get long quickly and so we group bits together and
use a larger base. 2 bits = base 4, 3 bits = base 8, 4 bits = base 16.
• The base 8 system (octal) uses the symbols: 0, 1, 2, 3, 4, 5, 6, 7. It
takes 3 binary bits to make an octal digit. Group the binary digits in
groups of 3 from the decimal point (the Least Significant Bit (LSB)).
• Octal gained popularity because some machines (such
as the PDP8) used 12-bit, 24-bit or 36-bit words and
numbers could be easily displayed in 4, 8, and 12 (octal)
digit displays. It was a cost and complexity saving. It is
also easy to learn.
258=010 1012 =2110
COSC 243 (Computer Architecture)
Lecture 2 - Data Representation 2
Bin Oct
000 0
001 1
010 2
011 3
100 4
101 5
110 6
111 7
23
Hexadecimal Number System
• Today machines use 8-bit, 16-bit, 32-bit or 64-bit words.
Using base 16 makes more sense. In hexadecimal (or
hex) each digit is 4 bits. To make up the “missing” digits
letters are used:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
• 4 bits (one digit) is a nybble. 2 nybbles is byte (8 bits).
Words are whole powers of bytes. Hardware makes more
sense today than it once did!
COSC 243 (Computer Architecture)
Lecture 2 - Data Representation 2
24
Hexadecimal Number System (cont)
• Take the binary number, e.g.:
100110110011111
• Group into chunks of 4 binary digits (right to left):
[0]100 1101 1001 1111
• Convert each group into its nybble:
4D9F
• The resulting number is the hex:
1001101100111112 → 4D9F16
COSC 243 (Computer Architecture)
Lecture 2 - Data Representation 2
Bin
Hex
Bin
Hex
0000
0
1000
8
0001
1
1001
9
0010
2
1010
A
0011
3
1011
B
0100
4
1100
C
0101
5
1101
D
0110
6
1110
E
0111
7
1111
F
25
Other Conversions
• Conversions:decimal→octal, decimal→hex,
octal→decimal, and hex→decimal can be
made using positional notation or similar
techniques as binary→decimal and
decimal→binary.
octal
decimal
binary
hex
COSC 243 (Computer Architecture)
Lecture 2 - Data Representation 2
26
Introduction to Binary Arithmetic
• Plus
– Exactly the same as base 10 addition
• Left to right:
13610
2710
====
16310
00112
01012
====
10002
6 +7
= 3 (carry 1)
3 + 2 + carry = 6 (carry 0)
1 + carry
=1
COSC 243 (Computer Architecture)
1+1
= 0 (carry 1)
1 + 0 + 1 = 0 (carry 1)
0 + 1 + 1 = 0 (carry 1)
0 + 0 + 1 = 1 (carry 0)
Lecture 2 - Data Representation 2
27
Introduction to Binary Arithmetic
• Minus
– Exactly the same as base 10 subtraction
• Left to right:
13610
2710
====
10910
10012
01012
====
01002
6-7
= 9 (borrow 10)
3 - 2 - 1 (borrow) = 0
1-0
=1
COSC 243 (Computer Architecture)
1-1
=0
0-0
=0
0-1
= 1 (borrow 2)
1 - 0 - 1 (borrow) = 0
Lecture 2 - Data Representation 2
28
Introduction to Negative Integers
• So far we have concentrated on positive integers
• Remember that the memory is a set of switches, there is
no decimal point and no negative sign, but we can devise
a scheme to represent negative numbers (we’ll do real
numbers in an upcoming lecture)
• 4 ways have been used:
–
–
–
–
Excess notation
Sign magnitude
One's complement
Two's complement
COSC 243 (Computer Architecture)
Lecture 2 - Data Representation 2
29
Excess Notation
• By convention we count from 0, but what if we count from
a different number. If we count from -3 (excess 3) then:
Number
-3
-2
-1
0
1
2
3
4
Bits
000
001
010
011
100
101
110
111
COSC 243 (Computer Architecture)
Lecture 2 - Data Representation 2
30
Excess Notation
• If we shift the numbers by about half the possible range
then we get about the same number of positive and
negative numbers
• Example:
– The computer stores integer values in 8 bits, using excess 128
notation. How is -6010 stored?
stored number
= -60 + 128
= 6810
= 0100 01002
COSC 243 (Computer Architecture)
Lecture 2 - Data Representation 2
31
Excess Notation Example 2
• The binary number, 01011101, is in excess 128 notation.
What is the number in decimal?
0101 1101 = 0 + 64 + 0 + 16 + 8 + 4 + 0 + 1
= 93 (excess 128)
decimal number
COSC 243 (Computer Architecture)
= 93 - 128
= -35
Lecture 2 - Data Representation 2
32
Excess Notation
• Most inconveniently, 0 (excess n) is not 0 in binary
• There must be a better way
• But, excess notation is used to store real numbers
– upcoming lecture
COSC 243 (Computer Architecture)
Lecture 2 - Data Representation 2
33
Sign Magnitude
• Why not just use the most significant bit to represent the
sign? This is so simple: 0 = positive, 1 = negative
• In 4 bits:
Bin
Value
Bin
Value
0000
0
1000
-0
0001
1
1001
-1
0010
2
1010
-2
0011
3
1011
-3
0100
4
1100
-4
0101
5
1101
-5
0110
6
1110
-6
0111
7
1111
-7
COSC 243 (Computer Architecture)
Lecture 2 - Data Representation 2
34
Sign Magnitude (cont)
• There are 2 values for 0! (+0 and -0)
• Addition and subtraction complicated
– Compute the bit pattern for 2 - 3
• The range of integers is (2n-1-1) to -(2n-1-1)
– Where n is the number of bits
– Which is 1 fewer than Excess notation
COSC 243 (Computer Architecture)
Lecture 2 - Data Representation 2
35
One’s Complement
• The negative is the complement of the positive
– If positive, do nothing
– If negative, complement (reverse) each bit
84 = 0101 0100
-84 = 1010 1011
COSC 243 (Computer Architecture)
Lecture 2 - Data Representation 2
36
One’s Complement
• Negation is easy
– Invert the bit-pattern
• Addition and subtraction is easier
– But there are still two values for zero
• Range of integers is (2n-1-1) to -(2n-1-1)
– Where n is the number of bits
•
Which is 1 fewer than Excess notation
COSC 243 (Computer Architecture)
Lecture 2 - Data Representation 2
37
Two’s Complement
• We want an encoding that:
–
–
–
–
–
Has one encoding for 0 (000)
Binary addition and subtraction works
Addition “through zero” works
Has a sign bit
Subtraction is addition of negative numbers
• To convert a positive to two’s complement
– Do nothing
• To convert a negative to two’s complement
– Complement (invert the bits) then add 1
Decimal
Binary
-4
100
-3
101
-2
110
-1
111
0
000
1
001
2
010
3
011
• The range of integers is (2n-1-1) to -(2n-1)
COSC 243 (Computer Architecture)
Lecture 2 - Data Representation 2
38
Example
• Convert -84 to two’s complement (using 8 bits)
84 =
0101 0100
• Complement and add 1
11
1010 1011
+
1
--------1010 1100
← carries
← each bit complemented
COSC 243 (Computer Architecture)
Lecture 2 - Data Representation 2
← -84 in two’s complement
39
Two’s Complement to Decimal
• If the high (sign) bit is 0
– Convert the binary number to decimal
• If the high (sign) bit is 1
–
–
–
–
Complement (invert) each bit
Add 1 to the binary number
Convert the binary number to decimal
Put a minus sign in front of the decimal number
• This is the same as subtracting the positive number from
2n where n is the number of bits in the representation
COSC 243 (Computer Architecture)
Lecture 2 - Data Representation 2
40
Example
• Convert the 8-bit number 1110 1010 in two’s complement to decimal
– Complement and add 1
1
0001 0101
+
1
--------0001 0110
<-- carry
<-- complement each bit
<-- add 1
0001 0110 = 16 + 4 + 2 = 22
• Answer = -22
COSC 243 (Computer Architecture)
Lecture 2 - Data Representation 2
41
Subtraction Example
• Subtract 5 from 18 in 8-bit, 2's complement
18 – 5 = 18 + -5
18 =
-5 =
0001 0010
1111 1011
---------10000 1101
• Dropping the high-order (9th) bit gives:
[1] 0000 1101 = 13
COSC 243 (Computer Architecture)
Lecture 2 - Data Representation 2
42
Two’s Complement Example 2
• Subtract 5 from -18 in 8-bit, 2's complement
-18 - 5 = -18 + -5
-18 =
-5 =
1110 1110
1111 1011
---------11110 1001
• Ignoring the high-order (9th) bit gives:
1110 1001 = -23
COSC 243 (Computer Architecture)
Lecture 2 - Data Representation 2
43
Two’s Complement Overflow
• The result is invalid if the addition (or subtraction) of two
positive numbers gives a negative number
– This is easy to check (the high bit is 1!)
• The result is invalid if the addition (or subtraction) of two
negative numbers gives a positive number
– This is easy to check (the high bit is 0!)
• These are (often) caught by the CPU as an overflow /
underflow exception
– This is then passed to your program as an exception
COSC 243 (Computer Architecture)
Lecture 2 - Data Representation 2
44
Homework I
• Convert 0.110 into binary (use up-to 16 digits)
– Is it (a) representable, (b) repeating, (c) irrational
• Convert 0.2510 into octal (use up-to 16 digits)
– Is it (a) representable, (b) repeating, (c) irrational
• Convert 0.310 into hexadecimal (use up-to 16 digits)
– Is it (a) representable, (b) repeating, (c) irrational
COSC 243 (Computer Architecture)
Lecture 2 - Data Representation 2
45
Homework II
+
=
10
The computer memory is a series of switches, we’ve broken
them into 4 bytes of 8 switches. Add the 4 x 8-bit binary
numbers to get a binary number. Convert that into base 10. Do
the same assuming two’s complement encoding.
COSC 243 (Computer Architecture)
Lecture 2 - Data Representation 2
46