Presentation is loading. Please wait.

Presentation is loading. Please wait.

IT253: Computer Organization

Similar presentations


Presentation on theme: "IT253: Computer Organization"— Presentation transcript:

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

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

3 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

4 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, 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 = = = Hexadecimal – There are 16 symbols (0-9A-F) 32 = = 2F = 5B0

5 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?

6 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?

7 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 = = 11 = 1*2^3 + 0*2^2 + 1*2^1 + 1*2^0 = 1011

8 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 2 / 2 = 1 with remainder 1 / 2 = 0 with remainder Now we just reverse the remainder numbers to get the binary = 39 in binary

9 Convert decimal to binary
Now we can check our work = 39 = 1*25 + 0*24 + 0*23 + 1*22 + 1*21 + 1*20 = = 39 So we have done our work correctly

10 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: = 0*2^5 + 1*2^4 + 0*2^3 + 1*2^2 + 0*2^1 + 1*2^0 = 21 Can you compute the decimal representation of ?

11 Powers of 2

12 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: = Octal numbers use 8 digits: {0 - 7} Example: = Hexadecimal numbers use 16 digits: {0-9, A-F} Example: = 04B016 = 0x04B0 does not distinguish between upper and lower case

13 Binary and Octal Easy to convert between the two
Group digits into groups of threes and change to octal 2^3 = 8

14 Binary to Hexadecimal Group binary into groups of four and change into hexadecimal symbols 2^4 = 16

15 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

16 Binary Integers Unsigned integers – means the binary number can be read without extra information 1111 = …… = 5 With 4 bits, what is the highest number that can be represented (15) How do we represent negative numbers?

17 Sign Magnitude Representation
Use the first bit of the number to represent the sign (positive or negative) of the number = -10 = 10 If the first bit is (1) then the number is negative. If the first bit is (0) then the number is positive

18 Advantages and Disadvantages
Simple extension, not hard to understand and decode There are equal numbers of positive and negative numbers Disadvantages Two representations of zero = = 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

19 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 = ; = > (positive -> negative) Still have two zeros though…

20 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

21 Advantages and Disadvantages of 2’s Complement
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

22 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: = -15 Unsigned example: = 31

23 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 = Positive numbers - add extra zeros to the front Negative numbers - add extra ones to the front Example = 100 (in 2’s complement) =

24 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 Step 3: Pad numbers

25 Conversion: Decimal to binary 2’s Complement
Example: Change 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 Step 3: Pad numbers Step 4: Because it's negative, we must invert and add 1 +1

26 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 = 15 = 13 11100 = 28 1’s Complement = 0 +1101 = -2 1100 = -2 2’s Complement = -1 +1101 = -3 1100 = -4

27 Binary Subtraction Think about it as adding two numbers where one of them has the sign changed 2’s Complement example 01111 (15)  (15) 01011 (11) (-11)  2’s Complement 00100 = 4  correct

28 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

29 Detecting Overflow Overflow occurs when the answer affects the sign:
Examples: Adding two positive numbers gives you a negative  PROBLEM 2) Adding two negatives gives a positive  PROBLEM

30 Detecting Overflow Operation First Number Overflow? A+B > 0 NO
Second Number Overflow? A+B > 0 NO <0 YES A-B >0 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.

31 Floating Point There are many numbers besides integers.
There is an infinite amount of numbers between just 0 and 1. Ex. .566, … How do computers represent these numbers, called floating points. (In programming they are used as "double" data types)

32 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.

33 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 x 26

34 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

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

36 Floating Point Example: X = -0.7510 in single precision (-.5 + -.25)
= = = = x 2-1 = x S = 1; Exp = = ; M = 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.

37 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 x 10^3 Because instead you would just write: 1.01 x 10^2 If it will always be there, why waste a bit?

38 Floating point More examples

39 Floating Point Example: 4.2 x 103 into binary 4.2 x 103 = 4200
4200 = Now “Normalize it” Need to move decimal point 12 places = x 212 So: Sign bit = 0 (because it’s positive) Exponent = = 139 = Significand = So the Answer is all three put together like: S Exp Significand

40 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 (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

41 Exponent Why do we care about 127?
When we find the exponent (like 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 = 130  Change to binary 130 =  Notice we don’t use signed numbers here for plus and minus

42 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

43 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.

44 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


Download ppt "IT253: Computer Organization"

Similar presentations


Ads by Google