Lecture2-DataRepresentation - Tonga Institute of Higher Education

Download Report

Transcript Lecture2-DataRepresentation - Tonga Institute of Higher Education

Tonga Institute of Higher Education
IT253: Computer Organization
Lecture 2:
Data Representation
Review
• Computers – the big picture
– Control, Datapath (from processor)
– Memory
– Input, Output
Data Representation
• We think about data and numbers in many different
ways.
– Add two hundred and ten plus fourteen
– 210+14
• The goal in computers is to use representation in
ways that are efficient and easy to manipulate
• Computers will store numbers and characters in
memory
• Computers need a way that is fast and compatible
with the nature of computers
• Thus, computers will represent data using other
number systems, such as binary or hexadecimal,
which are easier for computers to manipulate
Number Systems
• A number is a mathematical concept. It allows a person to
represent information (how many of something) in a compact
form.
• Instead of showing someone that you have ten pigs, you can
write “10”
• There are many ways to represent a number
– 10, X, 1010, A, 11111111111
• These symbols represent the same concept.
• Our goal is to understand the representation that computers use
to change and read data.
• This is generally called binary and hexadecimal.
• Binary means only two symbols (1,0) are used. All numbers can
be written using this system
– 5 = 101
- 7 = 111
67 = 1000011
• Hexadecimal – There are 16 symbols (0-9A-F)
– 32 = 10
47 = 2F
1456 = 5B0
Number Systems
• What we normally use is called decimal
– 15, 2543, 42, 18
• Decimal needs just 10 symbols in it to
represent all numbers (1,2,3,4,5,6,7,8,9,0)
• Binary has just two (0,1)
• Hexadecimal has 16 (0-9, A-F)
• Which number is the most “efficient” or
compact way to represent numbers?
Number Systems for Computers
• Today’s computers are built from transistors
• A transistor is a part that can be either off or on
• Thus, computers need to represent numbers using only
off and on
• The two symbols, off and on, can represent the digits 0
and 1
• A BIT is a Binary Digit (1 binary number)
• A bit can have a value of 0 or 1
• Binary representation
– weighted positional notation using base 2
– 1110 = 1*2^3 + 1*2^2 + 1*2^1 + 0*2^0 = 13
– 10011 = 1*2^4 + 1*2^1 + 1*2^0 = 19
• What is largest number, given 4 bits?
Conversion from decimal to binary
N is a positive Integer (in decimal representation)
bi i=0,...,k are the bits (binary digits) for the binary
representation of N
N = bk*2k +… +b2*22 + b1*21 + b0*20
binary representation: bk… b3b2b1b0
How do I compute b0?
Compute binary representation of 11?
• 11 = 8
+ 0
+ 2
+ 1 = 11
= 1*2^3 + 0*2^2 + 1*2^1 + 1*2^0
= 1011
Convert from decimal to binary
• Example
– 39
• 39 / 2 = 19 with remainder
• 19 / 2 = 9 with remainder
• 9 / 2 = 4 with remainder
• 4 / 2 = 2 with remainder
• 2 / 2 = 1 with remainder
• 1 / 2 = 0 with remainder
1
1
1
0
0
1
• Now we just reverse the remainder numbers
to get the binary
– 1 0 0 1 1 1 = 39 in binary
Convert decimal to binary
• Now we can check our work
– 100111 = 39
– 100111 = 1*25 + 0*24 + 0*23 + 1*22 + 1*21 + 1*20
– 100111 = 32 + 0
+ 0 + 4 + 2 +1 =
39
• So we have done our work correctly
Conversion from binary to
decimal
• N is a positive Integer (in decimal representation)
• bi i=0,...,k are the bits (binary digits) for the binary
representation of N
• example: 010101 = 0*2^5 + 1*2^4 + 0*2^3 + 1*2^2
+ 0*2^1 + 1*2^0
– 0 + 16 + 0 + 4 + 0 + 1 = 21
• Can you compute the decimal representation of
1110101?
Powers of 2
Number Systems
• Computers can input and output decimal numbers but
they convert them to an internal binary representation.
• Binary is good for computers, but hard for humans to
read
• Other numbers easily computed from binary…
• Binary numbers use only two different digits: {0,1}
– Example: 120010 = 000001001011000010
•
Octal numbers use 8 digits: {0 - 7}
– Example: 120010 = 042608
• Hexadecimal numbers use 16 digits: {0-9, A-F}
– Example: 120010 = 04B016 = 0x04B0
– does not distinguish between upper and lower case
Binary and Octal
• Easy to convert between the two
• Group digits into groups of threes and
change to octal 2^3 = 8
Binary to Hexadecimal
• Group binary into groups of four and
change into hexadecimal symbols
• 2^4 = 16
Binary Number Issues
• Complex arithmetic functions
• Negative numbers
• How large a number can be
represented with binary numbers
• Choose a method that is easy for
machines, not for humans
Binary Integers
• Unsigned integers – means the binary
number can be read without extra
information
– 1111 = 15
……
0101 = 5
• With 4 bits, what is the highest number
that can be represented (15)
• How do we represent negative
numbers?
Sign Magnitude Representation
• Use the first bit of the number to represent
the sign (positive or negative) of the number
1001010 = -10
0001010 = 10
• If the first bit is (1) then the number is
negative.
• If the first bit is (0) then the number is positive
Advantages and Disadvantages
• Advantages
– Simple extension, not hard to understand and
decode
– There are equal numbers of positive and negative
numbers
• Disadvantages
– Two representations of zero
• 10000 = 00000 = 0
• When we want to add the numbers together we must
make special cases for what sign it is. Makes it more
difficult for hardware
Better Method: 1’s Complement
• Method: use the largest binary
numbers to be negative
• To get a negative number, we
just invert a positive number
22 = 010110;
-22 = 101001
010110 -> 101001 (positive -> negative)
• Still have two zeros though…
Even Better: 2’s Complement
• Just like 1’s complement, except
to make a negative number, we
will invert a positive number and
add 1.
•Range: For 16 bit numbers
-32,768 – 32,767
Advantages and Disadvantages of
2’s Complement
• Advantages
– Only one representation of zero
– Addition algorithm will not depend on the
“sign” of a number
• Disadvantages
– One more negative number than there is
positive number.
-32 = 10000, but there is no way to show +32
Unsigned vs. Signed
• We have seen signed and unsigned numbers.
– When a number is signed, one bit will be
used to determine whether positive or
negative.
– Unsigned means that all bits are used to
store the number,
• There are no negative numbers,
• Can support twice as many positive numbers.
– Signed example: 11111 = -15
– Unsigned example: 11111 = 31
Sign Extension
• In a computer, all numbers will be
represented by a set amount of bits. Most
computers today have 32 bit numbers.
• What if your number doesn’t need 32 bits?
– Example 3 = 011 … what about other 29 bits?
– SIGN EXTEND =
00000000000000000000000000000011
– Positive numbers - add extra zeros to the front
– Negative numbers - add extra ones to the front
– Example -4 = 100 (in 2’s complement)
= 11111111111111111111111111111100
Conversion: Decimal to binary
2’s Complement
Example: Change 7510 to 2's comp. 16 bit binary number
Step 1: Divide to find binary
75/2 = 37 with Remainder 1
37/2 = 18 with Remainder 1
18/2 = 9 with Remainder 0
9/2 = 4 with Remainder 1
4/2 = 2 with Remainder 0
2/2 = 1 with Remainder 0
½ = 0 with Remainder 1
Step 2: Reverse numbers
1001011
Step 3: Pad numbers
0000 0000 0100 1011
Conversion: Decimal to binary
2’s Complement
Example: Change -7510 to 2's comp. 16 bit binary number
Step 1: Divide to find binary
75/2 = 37 with Remainder 1
37/2 = 18 with Remainder 1
18/2 = 9 with Remainder 0
9/2 = 4 with Remainder 1
4/2 = 2 with Remainder 0
2/2 = 1 with Remainder 0
½ = 0 with Remainder 1
Step 2: Reverse numbers
1001011
Step 3: Pad numbers
0000 0000 0100 1011
Step 4: Because it's negative, we must invert and add 1
1111 1111 1011 0100
+1
1111 1111 1011 0101
Binary Addition
• It's easy except must remember to carry 1’s.
• Also have to be aware of what format (unsigned, 1’s
complement, 2’s complement)
• It is important to remember that with all three formats you can
just add the numbers together and you will get the correct
answer
• Examples
Unsigned 1111 = 15
+1101 = 13
11100 = 28
1’s Complement
1111 = 0
+1101 = -2
1100 = -2
2’s Complement
1111 = -1
+1101 = -3
1100 = -4
Binary Subtraction
• Think about it as adding two numbers where
one of them has the sign changed
• 2’s Complement example
01111 (15)
- 01011 (11)

01111 (15)
+10101 (-11)  2’s Complement
00100 = 4  correct
Detecting Overflow
• “Overflow” is when the result of an
operation (add, sub, multiply, divide) is a
number that cannot be represented
within the number of allotted bits.
– If you multiply two 16 bit numbers, you will
get a number that is larger than 16 bits and
won’t be able to represent it with 16 bits
Detecting Overflow
Overflow occurs when the answer affects the sign:
Examples:
1) Adding two positive numbers gives you a negative
01101110
110
+0 1 0 0 0 1 0 0
+68
1 0110010
-78  PROBLEM
2) Adding two negatives gives a positive
10010 011
-109
+ 1 1 0 0 1 1 0 0 + -52
01011111
95  PROBLEM
Detecting Overflow
Operation
First Number
Second Number
Overflow?
A+B
>0
>0
NO
A+B
<0
<0
YES
A-B
>0
<0
NO
A-B
<0
>0
YES
Sometimes overflow is important to detect.
Sometimes we can ignore. Some programs will crash if an overflow
occurs. Some classes that you may use while programming may also
not allow for overflows and will return errors.
Floating Point
• There are many numbers besides integers.
• There is an infinite amount of numbers
between just 0 and 1.
– Ex. .566, .34001 …
• How do computers represent these numbers,
called floating points.
• (In programming they are used as "double"
data types)
Floating point
• Examples
3.0 x 108
2.66393 x 10-3
7.3922 x 101
Good for very small, very large, fractional or
irrational numbers.
Floating Point
• 3.86 x 108
– 3.86 called the “mantissa”
– 10 is the base or “radix”
– 8 is the “exponent”
– This number is base 10 (decimal). We
could also change the base to 2 (binary)
• Ex. 1.011 x 26
Floating Point
• Since computers have only 32 bits to
store numbers, we must save the
mantissa and exponent in a limited
space.
• If we give the mantissa more bits, then
we get greater accuracy.
• If we give the exponent more bits, then
we get a greater range
Floating Point
• The standard now is to give the
mantissa 23 bits and the exponent 8
bits and save 1 bit for the sign
Floating Point
Example: X = -0.7510 in single precision (-.5 + -.25)
-0.7510 = -.510 + -.2510 = -.12 + -.012 = -0.112
= -1.12 x 2-1 = -1.12 x 2126-127
S = 1; Exp = 12610 = 0111 11102;
M = 100 0000 0000 0000 0000 00002
The “significand”, or the “1” part of 1.xxxx, is always
assumed and does not need to be stored, because all
floating point numbers look like that.
The Lost One
• Notice the note on the bottom of the last
page.
• When we save the significand we do not
include the first “1”
• Why?
– Because every significand begins with a one.
– You never put 0.101 x 10^3
• Why?
– Because instead you would just write: 1.01 x 10^2
• If it will always be there, why waste a bit?
Floating point
• More examples
Floating Point
Example:
4.2 x 103 into binary
4.2 x 103 = 4200
4200 = 1000001101000
Now “Normalize it”
Need to move decimal point 12 places
= 1.000001101000 x 212
So:
Sign bit = 0 (because it’s positive)
Exponent = 12 + 127 = 139 = 10001011
Significand = 00000110100000000000000
So the Answer is all three put together like:
0 10001011 000001101000000000000
S
Exp
Significand
Exponent
• The hardest part to figure out is how to get the
Exponent.
• To understand what to do, we must first seek to
understand why we do it.
• The exponent of the number will be saved in 8 bits.
• If you have 8 bits then your range is 0-255 (2^8 =
256)
• But those are only positive numbers. You could also
have a negative exponent.
• So people decided to split up the range. If the
exponent bits were below 127 then it is negative
• If they are above 127 they are positive
• If there is no exponent (an exponent of 0), then you
just use 127
Exponent
• Why do we care about 127?
• When we find the exponent (like 1.0111 x 2^3
where the exponent is +3) then we add the
exponent to 127 to find the correct way to
save it.
• This way the computer knows that it is above
127 and it is a positive exponent
– Take 3+127 = 130  Change to binary
– 130 = 10000010  Notice we don’t use signed
numbers here for plus and minus
Complexities with Floating Points
• As well as overflow, there can be underflow,
where the number is too close to zero to be
represented.
• Accuracy is a big problem, especially for
irrational numbers. The becomes a problem
with rounding
• Many computers have special places in
memory, or separate processors to deal with
floating point numbers
• These are called FPU’s or floating point
processors
Basic Data Types
• We have discussed floating point and integers. Computers
also need to store characters (letters). This is used with a
format called ASCII. Each ASCII character is represented
with 7 bit ASCII code (shown below in octal)
• UNICODE is the newer standard with 16 bits.
Summary of Data
Representation
•
•
•
•
•
•
Number systems (decimal, binary, octal, hex)
Converting between number systems
Sign magnitude, 1 and 2’s complement
Overflow
Floating point
Basic Data types