44
IT253: Computer Organization Lecture 2: Data Representation Tonga Institute of Higher Education

IT253: Computer Organization Lecture 2: Data Representation Tonga Institute of Higher Education

Embed Size (px)

Citation preview

Page 1: IT253: Computer Organization Lecture 2: Data Representation Tonga Institute of Higher Education

IT253: Computer Organization

Lecture 2:

Data Representation

Tonga Institute of Higher Education

Page 2: IT253: Computer Organization Lecture 2: Data Representation Tonga Institute of Higher Education

Review

• Computers – the big picture– Control, Datapath (from processor)– Memory– Input, Output

Page 3: IT253: Computer Organization Lecture 2: Data Representation Tonga Institute of Higher Education

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

Page 4: IT253: Computer Organization Lecture 2: Data Representation Tonga Institute of Higher Education

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

Page 5: IT253: Computer Organization Lecture 2: Data Representation Tonga Institute of Higher Education

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?

Page 6: IT253: Computer Organization Lecture 2: Data Representation Tonga Institute of Higher Education

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?

Page 7: IT253: Computer Organization Lecture 2: Data Representation Tonga Institute of Higher Education

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

Page 8: IT253: Computer Organization Lecture 2: Data Representation Tonga Institute of Higher Education

Convert from decimal to binary

• Example– 39

• 39 / 2 = 19 with remainder 1• 19 / 2 = 9 with remainder 1• 9 / 2 = 4 with remainder 1• 4 / 2 = 2 with remainder 0• 2 / 2 = 1 with remainder 0• 1 / 2 = 0 with remainder 1

• Now we just reverse the remainder numbers to get the binary– 1 0 0 1 1 1 = 39 in binary

Page 9: IT253: Computer Organization Lecture 2: Data Representation Tonga Institute of Higher Education

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

Page 10: IT253: Computer Organization Lecture 2: Data Representation Tonga Institute of Higher Education

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?

Page 11: IT253: Computer Organization Lecture 2: Data Representation Tonga Institute of Higher Education

Powers of 2

Page 12: IT253: Computer Organization Lecture 2: Data Representation Tonga Institute of Higher Education

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

Page 13: IT253: Computer Organization Lecture 2: Data Representation Tonga Institute of Higher Education

Binary and Octal• Easy to convert between the two

• Group digits into groups of threes and change to octal 2^3 = 8

Page 14: IT253: Computer Organization Lecture 2: Data Representation Tonga Institute of Higher Education

Binary to Hexadecimal

• Group binary into groups of four and change into hexadecimal symbols

• 2^4 = 16

Page 15: IT253: Computer Organization Lecture 2: Data Representation Tonga Institute of Higher Education

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

Page 16: IT253: Computer Organization Lecture 2: Data Representation Tonga Institute of Higher Education

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?

Page 17: IT253: Computer Organization Lecture 2: Data Representation Tonga Institute of Higher Education

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

Page 18: IT253: Computer Organization Lecture 2: Data Representation Tonga Institute of Higher Education

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

Page 19: IT253: Computer Organization Lecture 2: Data Representation Tonga Institute of Higher Education

Better Method: 1’s Complement

• Method: use the largest binary

numbers to be negative

• To get a negative number, we

just invert a positive number22 = 010110; -22 = 101001

010110 -> 101001 (positive -> negative)

• Still have two zeros though…

Page 20: IT253: Computer Organization Lecture 2: Data Representation Tonga Institute of Higher Education

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

Page 21: IT253: Computer Organization Lecture 2: Data Representation Tonga Institute of Higher Education

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

Page 22: IT253: Computer Organization Lecture 2: Data Representation Tonga Institute of Higher Education

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

Page 23: IT253: Computer Organization Lecture 2: Data Representation Tonga Institute of Higher Education

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

Page 24: IT253: Computer Organization Lecture 2: Data Representation Tonga Institute of Higher Education

Conversion: Decimal to binary2’s Complement

Example: Change 7510 to 2's comp. 16 bit binary numberStep 1: Divide to find binary

75/2 = 37 with Remainder 137/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 1Step 2: Reverse numbers

1001011Step 3: Pad numbers

0000 0000 0100 1011

Page 25: IT253: Computer Organization Lecture 2: Data Representation Tonga Institute of Higher Education

Conversion: Decimal to binary2’s Complement

Example: Change -7510 to 2's comp. 16 bit binary numberStep 1: Divide to find binary

75/2 = 37 with Remainder 137/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 1Step 2: Reverse numbers

1001011Step 3: Pad numbers

0000 0000 0100 1011Step 4: Because it's negative, we must invert and add 1

1111 1111 1011 0100+1

1111 1111 1011 0101

Page 26: IT253: Computer Organization Lecture 2: Data Representation Tonga Institute of Higher Education

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 = 1311100 = 28

1’s Complement 1111 = 0+1101 = -2 1100 = -2

2’s Complement 1111 = -1+1101 = -3 1100 = -4

Page 27: IT253: Computer Organization Lecture 2: Data Representation Tonga Institute of Higher Education

Binary Subtraction

• Think about it as adding two numbers where one of them has the sign changed

• 2’s Complement example 01111 (15) 01111 (15)- 01011 (11) +10101 (-11) 2’s Complement

00100 = 4 correct

Page 28: IT253: Computer Organization Lecture 2: Data Representation Tonga Institute of Higher Education

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

Page 29: IT253: Computer Organization Lecture 2: Data Representation Tonga Institute of Higher Education

Detecting Overflow

Overflow occurs when the answer affects the sign:Examples:

1) Adding two positive numbers gives you a negative0 1 1 0 1 1 1 0 110

+0 1 0 0 0 1 0 0 +68 1 0 1 1 0 0 1 0 -78 PROBLEM

2) Adding two negatives gives a positive 1 0 0 1 0 0 1 1 -109 + 1 1 0 0 1 1 0 0 + -52

0 1 0 1 1 1 1 1 95 PROBLEM

Page 30: IT253: Computer Organization Lecture 2: Data Representation Tonga Institute of Higher Education

Detecting Overflow

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.

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

Page 31: IT253: Computer Organization Lecture 2: Data Representation Tonga Institute of Higher Education

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)

Page 32: IT253: Computer Organization Lecture 2: Data Representation Tonga Institute of Higher Education

Floating point

• Examples3.0 x 108

2.66393 x 10-3

7.3922 x 101

Good for very small, very large, fractional or irrational numbers.

Page 33: IT253: Computer Organization Lecture 2: Data Representation Tonga Institute of Higher Education

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

Page 34: IT253: Computer Organization Lecture 2: Data Representation Tonga Institute of Higher Education

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

Page 35: IT253: Computer Organization Lecture 2: Data Representation Tonga Institute of Higher Education

Floating Point

• The standard now is to give the mantissa 23 bits and the exponent 8 bits and save 1 bit for the sign

Page 36: IT253: Computer Organization Lecture 2: Data Representation Tonga Institute of Higher Education

Floating Point

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.

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

Page 37: IT253: Computer Organization Lecture 2: Data Representation Tonga Institute of Higher Education

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?

Page 38: IT253: Computer Organization Lecture 2: Data Representation Tonga Institute of Higher Education

Floating point• More examples

Page 39: IT253: Computer Organization Lecture 2: Data Representation Tonga Institute of Higher Education

Floating PointExample:

4.2 x 103 into binary4.2 x 103 = 42004200 = 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 = 10001011Significand = 00000110100000000000000

So the Answer is all three put together like:0 10001011 000001101000000000000S Exp Significand

Page 40: IT253: Computer Organization Lecture 2: Data Representation Tonga Institute of Higher Education

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

Page 41: IT253: Computer Organization Lecture 2: Data Representation Tonga Institute of Higher Education

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

Page 42: IT253: Computer Organization Lecture 2: Data Representation Tonga Institute of Higher Education

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

Page 43: IT253: Computer Organization Lecture 2: Data Representation Tonga Institute of Higher Education

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.

Page 44: IT253: Computer Organization Lecture 2: Data Representation Tonga Institute of Higher Education

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