Top Banner
1 CS 135: Computer Architecture I CS 135: Computer Architecture I Instructor: Prof Bhagi Narahari Instructor: Prof. Bhagi Narahari Dept. of Computer Science Course URL: www.seas.gwu.edu/~bhagiweb/cs135/ Number Representation Chapter 2 of Text (P&P): Today, & next class. Datatypes of the machine Integer Datatypes - Unsigned & Signed 2's Complement Integers Bi D i lC i CS 135 Binary-Decimal Conversion Text: ASCII, Hex Floating point representation Arithmetic and Logic Operations on Bits Sign Extension and Overflow C datatypes and machine representation Course Trivia…Important CS 143 “tutorial” on Saturday 10am Will get you up to speed with some systems issues: Using compilers Unix CS 135 Unix editors Recall: what are Computers meant to do ? We will be solving problems that are describable in English (or Greek or French or Hindi or Chinese or ...) and using a box filled with electrons and magnetism to accomplish the task. CS 135 This is accomplished using a system of well defined (sometimes) transformations that have been developed over the last 50+ years.
36

Recall: what are Computers meant to do - George …bhagiweb/cs135/lectures... ·  · 2010-09-022 Problem Transformation - levels of abstraction Natural Language Algorithm Program

Mar 31, 2018

Download

Documents

dangthuy
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Recall: what are Computers meant to do - George …bhagiweb/cs135/lectures... ·  · 2010-09-022 Problem Transformation - levels of abstraction Natural Language Algorithm Program

1

CS 135: Computer Architecture ICS 135: Computer Architecture I

Instructor: Prof Bhagi NarahariInstructor: Prof. Bhagi NarahariDept. of Computer Science

Course URL: www.seas.gwu.edu/~bhagiweb/cs135/

Number Representation

Chapter 2 of Text (P&P): Today, & next class.

Datatypes of the machine Integer Datatypes - Unsigned & Signed

2's Complement Integers

Bi D i l C i

CS 135

Binary-Decimal Conversion

Text: ASCII, Hex

Floating point representation

Arithmetic and Logic Operations on Bits Sign Extension and Overflow

C datatypes and machine representation

Course Trivia…Important

CS 143 “tutorial” on Saturday 10am Will get you up to speed with some systems

issues: Using compilers

Unix

CS 135

Unix

editors

Recall: what are Computers meant to do ?

We will be solving problems that are describable in English (or Greek or French or Hindi or Chinese or ...) and using a box filled with electrons and magnetism to accomplish the task.

CS 135

This is accomplished using a system of well defined (sometimes) transformations that have been developed over the last 50+ years.

Page 2: Recall: what are Computers meant to do - George …bhagiweb/cs135/lectures... ·  · 2010-09-022 Problem Transformation - levels of abstraction Natural Language Algorithm Program

2

Problem Transformation- levels of abstraction

Natural Language

Algorithm

Program

The desired behavior:the application

CS 135

Machine Architecture

Devices

Micro-architecture

Logic Circuits

The building blocks: electronic devices

Representing Information/Data

Our first requirement is to find a way to represent information (data) in a form that is mutually comprehensible by human and machine. What kinds of data ?

Integers

Reals

Text

L i l

CS 135

Logical

sound

Images

Video

We will start by examining different ways of representing integers, and look for a form that suits the computer.

Datatypes

A type is a classification of data that tells the compiler or interpreter how the programmer intends to use it. the process/algorithm and result of adding two

variables differs greatly according to whether they are integers, floating point numbers, or strings.

CS 135

Patt: "We say a particular representation is a datatype if there are operations in the computer that can operate on information that is encoded in that representation.“

Programming languages have a set of data types defined in the language In C : float, int, long int, unsigned int

Machine Representation

Computers store variables (data)

We/program-logic reason about numbers in many different ways Datatype may restrict what we can do

Example ??

CS 135

p

Key is to use a representation that is amenable to hardware implementation and is “efficient” What do we mean by efficient ??

Page 3: Recall: what are Computers meant to do - George …bhagiweb/cs135/lectures... ·  · 2010-09-022 Problem Transformation - levels of abstraction Natural Language Algorithm Program

3

Data Representation in a Machine

Question: How do computers store numbers

when you write in a program:

int x; float y;

x=10;

CS 135

x=10;

y= 0.34;

What do the variables x and y actually hold ??? Later question: where are x and y stored?

Number systems

A number is a mathematical concept Natural numbers, Integers, Reals, Rationals,..

Many ways to represent a number….. Symbols used to create a representation

CS 135

Simplest number representation ? What did you (as an infant) start with ?

Also used by Turing machine….

Radix-k notation

Use k symbols – also known as k-ary numbers (radix k) 0,1,2,…k-1 Radix-10 (decimal) 0,1,2,…,9 Radix 2 (binary) 0,1 Radix 16 (hex) 0 1 9 A B C D E F

CS 135

Radix 16 (hex) 0,1,…,9,A,B,C,D,E,F Weighted positional numbers – position

gives “weight” of location

How many different radix k numbers of length n Recall CS 123 –Discrete Math:

Each of the n positions can have k values

Weighted Positional Numbers

“decimal” means that we have ten digits to use in

our representation (the symbols 0 through 9)

Weighted positional: “weight” of digit depends on its

position

CS 135

What is 3,546?

How about negative numbers?

Page 4: Recall: what are Computers meant to do - George …bhagiweb/cs135/lectures... ·  · 2010-09-022 Problem Transformation - levels of abstraction Natural Language Algorithm Program

4

Binary Representation

Weighted positional notation using base (radix) 2

What are the symbols in base 2 ?

k bit number: b b b b

CS 135

k bit number: bk-1,bk-2,…,b1,b0

Each bi is ?

Decimal integer N for this binary number is:

N =

Binary Numbers

1110 = (???)2

0112 = (??)10

H diff t b b

CS 135

How many different numbers can be represented using N bits ?

What is the largest decimal number representable using N binary bits ?

Conversion from Decimal to Binary

//input is Decimal number N, output is list of bits bi //

i=0;

while N > 0 do

bi = N % 2; // bi = remainder; N mod 2

N = N / 2; // N becomes quotient of division

i++;

CS 135

end while

Replace 2 by r and you have an algorithm that computes the base r representation for N

Unsigned Binary Arithmetic

Base-2 addition – just like base-10! add from right to left, propagating carry

10010 10010 1111+ 1001 + 1011 + 1

carry

CS 135

11011 11101 10000

10111+ 111

Subtraction, multiplication, division,…

Page 5: Recall: what are Computers meant to do - George …bhagiweb/cs135/lectures... ·  · 2010-09-022 Problem Transformation - levels of abstraction Natural Language Algorithm Program

5

Question

Why do we bother with bits? Why not simply use a decimal system? Base 10 Number Representation

fingers are called “digits”

Natural representation for financial transactions Floating point number cannot exactly represent $1.20

CS 135

Even carries through in scientific notation 1.5213 X 104

Why Don’t Computers Use Base 10?

Implementing Electronically Hard to store

ENIAC (First electronic computer) used 10 vacuum tubes / digit

Hard to transmit Need high precision to encode 10 signal levels on single wire

Messy to implement digital logic functions

CS 135

Messy to implement digital logic functions Addition, multiplication, etc.

How do we represent data in a computer?

At the lowest level, a computer is an electronic machine. works by controlling the flow of electrons

Easy to recognize two conditions:1. presence of a voltage – we’ll call this state “1”

2. absence of a voltage – we’ll call this state “0”

CS 135

2. absence of a voltage we ll call this state 0

•Do you need any special hardware to test 0 or 1 in your wall socket ?

•Could base state on value of voltage, but control and detection circuits more complex.

compare turning on a light switch tomeasuring or regulating voltage

Computer is a binary digital system.

Binary (base two) system:• has two states: 0 and 1

Digital system:• finite number of symbols

CS 135

Basic unit of information is the binary digit, or bit.

Values with more than two states require multiple bits. A collection of two bits has four possible states:

00, 01, 10, 11

A collection of three bits has eight possible states:

000, 001, 010, 011, 100, 101, 110, 111

A collection of n bits has 2n possible states.

Page 6: Recall: what are Computers meant to do - George …bhagiweb/cs135/lectures... ·  · 2010-09-022 Problem Transformation - levels of abstraction Natural Language Algorithm Program

6

Machine Data Types

devices that make up a computer are switches that can be on or off, i.e. at high or low voltage.

CS 135

Thus they naturally provide us with two symbols to work with: we can call them on & off, or (more usefully) 0 and 1.

Course Adminis-trivia

Labs and lab exercises/assignments start next week Lab exercise given out at start of lab

Finish it in first part of lab; if you cannot then you can turn it in within 24 hours.

Teams assigned

CS 135

Will change teams at least once

office hours – go to those! Homework 1 and Team homework 1 – will

be posted Tuesday. Quiz 1 will be next week- on Thursday

Sept.9th at start of class 2:20pm If you come late then you will miss the quiz.

Binary Representation…more to consider

We now have a model for representing natural numbers using binary notation

Is this all we need for all ‘applications’ ? Do you need to work with other types of

numbers/data ?

CS 135

Is this enough to represent all integers ?

Unsigned Binary Integers

3-bits 5-bits 8-bits

0 000 00000 00000000

Y = “abc” = a.22 + b.21 + c.20

N = number of bits

R i

(where the digits a, b, c can each take on the values of 0 or 1 only)

CS 135

1 001 00001 00000001

2 010 00010 00000010

3 011 00011 00000011

4 100 00100 00000100

Range is:0 i < 2N - 1

Problem:

• How do we represent

negative numbers?

Page 7: Recall: what are Computers meant to do - George …bhagiweb/cs135/lectures... ·  · 2010-09-022 Problem Transformation - levels of abstraction Natural Language Algorithm Program

7

Signed Integers

Signed magnitude Easy to understand

Fun to be with

Not so good with hardware

1’s Complement

CS 135

1 s Complement To get negative Just flip all the bits

Used in some early computers

Signed Magnitude

Leading bit is the sign bit -4 10100

-3 10011

-2 10010

-1 10001Y = “abc” = (-1)a (b.21 + c.20)

CS 135

-0 10000

+0 00000

+1 00001

+2 00010

+3 00011

+4 00100

Range is:-2N-1 + 1 < i < 2N-1 - 1

Problems: ?

Signed Magnitude

Leading bit is the sign bit -4 10100

-3 10011

-2 10010

-1 10001Y = “abc” = (-1)a (b.21 + c.20)

CS 135

-0 10000

+0 00000

+1 00001

+2 00010

+3 00011

+4 00100

Range is:-2N-1 + 1 < i < 2N-1 - 1

Problems:

• How do we do addition/subtraction?

•What is 2 + (-1) ?

• We have two numbers for zero (+/-)!

One’s Complement

Invert all bits -4 11011

-3 11100

-2 11101

-1 11110

If msb (most significant bit) is 1 then thenumber is negative (same as signed magnitude)

CS 135

-0 11111

+0 00000

+1 00001

+2 00010

+3 00011

+4 00100

Range is:-2N-1 + 1 < i < 2N-1 - 1

Problems: ?

Page 8: Recall: what are Computers meant to do - George …bhagiweb/cs135/lectures... ·  · 2010-09-022 Problem Transformation - levels of abstraction Natural Language Algorithm Program

8

One’s Complement

Invert all bits -4 11011

-3 11100

-2 11101

-1 11110

If msb (most significant bit) is 1 then thenumber is negative (same as signed magnitude)

CS 135

-0 11111

+0 00000

+1 00001

+2 00010

+3 00011

+4 00100

Range is:-2N-1 + 1 < i < 2N-1 - 1

Problems:

•What is 2 + (-1) ?

•Same as for signed magnitude

Reality Sets In

Any arbitrary scheme of allowing different bit patterns to represent different numbers could be made to work but...

Problems with sign-magnitude and 1’s l t

CS 135

complement two representations of zero (+0 and –0)

arithmetic circuits are complex How to add two sign-magnitude numbers?

e.g., try 2 + (-1)

How to add to one’s complement numbers? e.g., try 2 + (-1)

Two’s Complement2's complement

Used in almost all computers manufactured today

Allows easy to implement math operations

How does it work?

Two’s complement representation developed to make circuits easy for arithmetic.

for each positive number (X), assign value to its negative ( X)

CS 135

negative (-X),such that X + (-X) = 0 with “normal” addition, ignoring carry out

00101 (5) 01001 (9)

+ 11011 (-5) + (-9)

00000 (0) 00000 (0)

Two’s Complement Representation

If number is positive or zero, normal binary representation, zeroes in upper bit(s)

If number is negative, start with positive number

flip every bit (i.e., take the one’s complement)

then add one

CS 135

then add one

00101 (5) 01001 (9)

11010 (1’s comp) (1’s comp)

+ 1 + 111011 (-5) (-9)

Page 9: Recall: what are Computers meant to do - George …bhagiweb/cs135/lectures... ·  · 2010-09-022 Problem Transformation - levels of abstraction Natural Language Algorithm Program

9

Two’s Complement

Transformation To transform a into -a, invert all

bits in a and add 1 to the result

-16 10000

… …

-3 11101

-2 11110Range is:

CS 135

-1 11111

0 00000

+1 00001

+2 00010

+3 00011

… …

+15 01111

Range is:-2N-1 < i < 2N-1 - 1

Advantages:

• Operations need not check the sign

• Only one representation for zero

• Efficient use of all the bits

Two’s Complement Shortcut

To take the two’s complement of a number: copy bits from right to left until (and including) the first

“1”

flip remaining bits to the left

011010000 011010000

CS 135

100101111 (1’s comp)

+ 1100110000 100110000

(copy)(flip)

Question to think about: Why does this trick work ?

Unsigned

012345

Binary

000000010010001101000101

SignedMag

012345

1'sComp

012345

2'sComp

012345

N = 4 Number Represented

CS 135

56789101112131415

01010110011110001001101010111100110111101111

567-0-1-2-3-4-5-6-7

567-7-6-5-4-3-2-1-0

567-8-7-6-5-4-3-2-1

Converting Binary (2’s C) to Decimal

1. If leading bit is one, take two’s complement to get a positive number.

2. Add powers of 2 that have “1” in thecorresponding bit positions.

3. If original number was negative,

n 2n

0 1

1 2

2 4

CS 135

3. If original number was negative,add a minus sign. 3 8

4 16

5 32

6 64

7 128

8 256

9 512

10 1024

X = 01101000two

= 26+25+23 = 64+32+8= 104ten

Assuming 8-bit 2’s complement numbers.

Page 10: Recall: what are Computers meant to do - George …bhagiweb/cs135/lectures... ·  · 2010-09-022 Problem Transformation - levels of abstraction Natural Language Algorithm Program

10

More Examples

n 2n

0 1

1 2

2 4

X = 00100111two

==

X = 11100110

CS 135

3 8

4 16

5 32

6 64

7 128

8 256

9 512

10 1024

Assuming 8-bit 2’s complement numbers.

X = 11100110two

-X ===

X =

Converting Decimal to Binary (2’s C)

First Method: Division1. Find magnitude of decimal number. (Always

positive.)

2. Divide by two – remainder is least significant bit.

3 K di idi b t til i

CS 135

3. Keep dividing by two until answer is zero,writing remainders from right to left.

4. Append a zero as the MS bit;if original number was negative, take two’s complement.

Question to think about: what is going on here?

Converting Decimal to Binary (2’s C)

X = 104ten 104/2 = 52 r0 bit 052/2 = 26 r0 bit 126/2 = 13 r0 bit 213/2 = 6 r1 bit 3

6/2 = 3 r0 bit 4

CS 135

6/2 = 3 r0 bit 43/2 = 1 r1 bit 5

X = 01101000two 1/2 = 0 r1 bit 6

Encoding Integers

short int x = 15213;short int y = -15213;

C short 2 bytes long

B2T (X ) xw1 2w1 xi 2

i

i0

w2

B2U(X ) xi 2i

i0

w1

Unsigned Two’s Complement

SignBit

CS 135

y g

Sign Bit For 2’s complement, most significant bit indicates

sign 0 for nonnegative

1 for negative

Decimal Hex Binaryx 15213 3B 6D 00111011 01101101y -15213 C4 93 11000100 10010011

Page 11: Recall: what are Computers meant to do - George …bhagiweb/cs135/lectures... ·  · 2010-09-022 Problem Transformation - levels of abstraction Natural Language Algorithm Program

11

Encoding Example (Cont.)

x = 15213: 00111011 01101101y = -15213: 11000100 10010011

Weight 15213 -152131 1 1 1 12 0 0 1 24 1 4 0 08 1 8 0 0

16 0 0 1 16

CS 135

32 1 32 0 064 1 64 0 0

128 0 0 1 128256 1 256 0 0512 1 512 0 0

1024 0 0 1 10242048 1 2048 0 04096 1 4096 0 08192 1 8192 0 0

16384 0 0 1 16384-32768 0 0 1 -32768

Sum 15213 -15213

Numeric RangesNumeric Ranges

Unsigned Values

UMin = 0000…0

UMax = 2w – 1111…1

Two’s Complement Values

TMin = –2w–1

100…0

TMax = 2w–1 – 1011…1

CS 135

Other Values

Minus 1111…1Decimal Hex Binary

UMax 65535 FF FF 11111111 11111111TMax 32767 7F FF 01111111 11111111TMin -32768 80 00 10000000 00000000-1 -1 FF FF 11111111 111111110 0 00 00 00000000 00000000

Values for W = 16

Values for Different Word Sizes

Observations C Programming

W8 16 32 64

UMax 255 65,535 4,294,967,295 18,446,744,073,709,551,615TMax 127 32,767 2,147,483,647 9,223,372,036,854,775,807TMin -128 -32,768 -2,147,483,648 -9,223,372,036,854,775,808

CS 135

Obse at o s |TMin | = TMax +

1 Asymmetric range

UMax = 2 * TMax+ 1

g g #include <limits.h>

K&R App. B11

Declares constants, e.g., ULONG_MAX

LONG_MAX

LONG_MIN

Values platform-specific

Sign Extension

Suppose we have a number which is stored in a four bit register

We wish to add this number to a number stored in a eight bit register

We have a device which will do the

CS 135

addition and it is designed to add two 8 bit numbers

What issues do we need to deal with?

Page 12: Recall: what are Computers meant to do - George …bhagiweb/cs135/lectures... ·  · 2010-09-022 Problem Transformation - levels of abstraction Natural Language Algorithm Program

12

Sign Extension

To add two numbers, we must represent themwith the same number of bits.

If we just pad with zeroes on the left:4-bit 8-bit0100 (4) 00000100 (still 4)

1100 (-4) 00001100 (12, not -4)

CS 135

( ) ( , )

Sign Extension

To add two numbers, we must represent themwith the same number of bits.

If we just pad with zeroes on the left:4-bit 8-bit0100 (4) 00000100 (still 4)

1100 (-4) 00001100 (12, not -4)

CS 135

Instead, replicate the MS bit -- the sign bit:

( ) ( , )

4-bit 8-bit0100 (4) 00000100 (still 4)

1100 (-4) 11111100 (still -4)

Question to think about: why does this work?

Other Data Types

Other numeric data typese.g. Hex, BCD – binary coded decimal

Bit vectors & maskssometimes we want to deal with the individual bits themselves

Floating Point – for real numbers

CS 135

g

Text representationsASCII: uses 8 bits to represent main Western alphabetic

characters & symbols, plus several “control codes”,

Unicode: 16 bit superset of ASCII providing representation of many different alphabets and specialized symbol sets.

EBCDIC: IBM’s mainframe representation.

Reading binary strings

Using binary numbers is both a blessing a curse! One can examine directly any particular bit

Reading, writing, etc. prone to error

What is easier to read and remember

CS 135

01101010

6A

Page 13: Recall: what are Computers meant to do - George …bhagiweb/cs135/lectures... ·  · 2010-09-022 Problem Transformation - levels of abstraction Natural Language Algorithm Program

13

Solution

Group bits and assign a single digit to represent each group

How big should each group be?

CS 135

Hexadecimal Notation

It is often convenient to write binary (base-2) numbersas hexadecimal (base-16) numbers instead.

fewer digits -- four bits per hex digit

less error prone -- easy to corrupt long string of 1’s and 0’s

Binary Hex Decimal0000 0 0

Binary Hex Decimal1000 8 8

CS 135

0000 0 0

0001 1 1

0010 2 2

0011 3 3

0100 4 4

0101 5 5

0110 6 6

0111 7 7

1000 8 8

1001 9 9

1010 A 10

1011 B 11

1100 C 12

1101 D 13

1110 E 14

1111 F 15

Converting from Binary to Hexadecimal

Every four bits is a hex digit. start grouping from right-hand side

011101010001111010011010111

CS 135

7D4F8A3

This is not a new machine representation,just a convenient way to write the number.

Hexadecimal Notation

Number of k length represented by string of HEX symbols hk-1,hk-2,…,h1,h0

Each hi has valued between 0 and F

A=10, B=11, C=12, D=13, E=14, F=15

CS 135

Decimal equivalent N is: N= hk-1*16k-1 + …+ h1*161 + h0*160

Eg: 2B7 = 2*162 + 11*161 + 7*160

also denoted as x2B7

Page 14: Recall: what are Computers meant to do - George …bhagiweb/cs135/lectures... ·  · 2010-09-022 Problem Transformation - levels of abstraction Natural Language Algorithm Program

14

ASCII Codes

Represent characters from keyboard This encoding used to transfer characters

between computer and all peripherals (keyboard, disk, network…)

ASCII: American Standard Code for Information Interchange

CS 135

Information Interchange 7 bits needed to encode all characters

Represent as 8 bit number

Typing a key on keyboard = corresponding 8-bit ASCII code is stored and sent to computer

Text: ASCII Characters

ASCII: Maps 128 characters to 7-bit code. both printable and non-printable (ESC, DEL, …) characters

00 nul 10 dle 20 sp 30 0 40 @ 50 P 60 ` 70 p

01 soh 11 dc1 21 ! 31 1 41 A 51 Q 61 a 71 q

02 stx 12 dc2 22 " 32 2 42 B 52 R 62 b 72 r

03 etx 13 dc3 23 # 33 3 43 C 53 S 63 c 73 s

04 eot 14 dc4 24 $ 34 4 44 D 54 T 64 d 74 t

CS 135

04 eot 14 dc4 24 $ 34 4 44 D 54 T 64 d 74 t

05 enq 15 nak 25 % 35 5 45 E 55 U 65 e 75 u

06 ack 16 syn 26 & 36 6 46 F 56 V 66 f 76 v

07 bel 17 etb 27 ' 37 7 47 G 57 W 67 g 77 w

08 bs 18 can 28 ( 38 8 48 H 58 X 68 h 78 x

09 ht 19 em 29 ) 39 9 49 I 59 Y 69 i 79 y0a nl 1a sub 2a * 3a : 4a J 5a Z 6a j 7a z

0b vt 1b esc 2b + 3b ; 4b K 5b [ 6b k 7b {

0c np 1c fs 2c , 3c < 4c L 5c \ 6c l 7c |

0d cr 1d gs 2d - 3d = 4d M 5d ] 6d m 7d }

0e so 1e rs 2e . 3e > 4e N 5e ^ 6e n 7e ~

0f si 1f us 2f / 3f ? 4f O 5f _ 6f o 7f del

Interesting Properties of ASCII Code

What is relationship between a decimal digit ('0', '1', …)and its ASCII code?

What is the difference between an upper-case letter ('A', 'B', …) and its lower-case equivalent ('a', 'b', …)?

CS 135

Given two ASCII characters, how do we tell which comes first in alphabetical order?

Are 128 characters enough?(http://www.unicode.org/)

No new operations -- integer arithmetic and logic.

Limitations of integer representations ?..do we need anything else?

Most numbers are not integer!Even with integers, there are other considerations

Range:The magnitude of the numbers we can represent is determined

CS 135

g pby how many bits we use: e.g. with 32 bits the largest number we can represent is about +/- 2 billion, far too small for

many purposes.

Precision:The exactness with which we can specify a number:

e.g. a 32 bit number gives us 31 bits of precision, or roughly 9 figure precision in decimal repesentation.

How to deal with Real numbers…We need other data types!

Page 15: Recall: what are Computers meant to do - George …bhagiweb/cs135/lectures... ·  · 2010-09-022 Problem Transformation - levels of abstraction Natural Language Algorithm Program

15

How to deal with complicated real numbers….Some History…

The Indiana Legislature once introduced legislation declaring that the value of was exactly 3.2

CS 135

Scientific Notation

-6.023 x 10-23

Sign ofExponent

CS 135

Sign

NormalizedMantissa

Base

Exponent

??

Binary Floating Point Representation

Same basic idea as scientific notation

Modifications and improvments based on Hardware architecture

Efficiency (Space & Time)

Additional requirements

CS 135

Additional requirements Infinity

Not a number

Not normalized

etc.

IEEE-754

0 00000000 00000000000000000000000

sig

exponent mantissa (significand)

31 30 23 22 0

CS 135

n (-1)S * 1.M * 2 E-127

Sign

1 is understood

Mantissa (w/o leading 1)

Base

Biased Exponent

Page 16: Recall: what are Computers meant to do - George …bhagiweb/cs135/lectures... ·  · 2010-09-022 Problem Transformation - levels of abstraction Natural Language Algorithm Program

16

CS 135: Computer Architecture ICS 135: Computer Architecture I

Instructor: Prof Bhagi NarahariInstructor: Prof. Bhagi NarahariDept. of Computer Science

Course URL: www.seas.gwu.edu/~bhagiweb/cs135/

Operations: Arithmetic and Logical

Recall: a data type includes representation and operations.

We now have a good representation for signed integers, so let’s look at some arithmetic operations:

Addition

Subtraction

CS 135

Sign Extension

overflow conditions for addition.

Multiplication, division, etc., can be built from these basic operations.

Logical operations are also useful: AND

OR

NOT

Addition

As we’ve discussed, 2’s comp. addition is just binary addition. assume all integers have the same number of bits

ignore carry out

for now, assume that sum fits in n-bit 2’s comp. t ti

CS 135

representation

01101000 (104) 11110110 (-10)

+ 11110000 (-16) + (-9)

01011000 (98) (-19)

Assuming 8-bit 2’s complement numbers.

What happens when we add a number to itself?

CS 135

Page 17: Recall: what are Computers meant to do - George …bhagiweb/cs135/lectures... ·  · 2010-09-022 Problem Transformation - levels of abstraction Natural Language Algorithm Program

17

Subtraction

Negate subtrahend (2nd no.) and add. assume all integers have the same number of bits

ignore carry out

for now, assume that difference fits in n-bit 2’s comp. representation

CS 135

01101000 (104) 11110110 (-10)

- 00010000 (16) - (-9)

01101000 (104) 11110110 (-10)

+ 11110000 (-16) + (9)

01011000 (88) (-1)

Assuming 8-bit 2’s complement numbers.

Example..

From team hw1: (i)

CS 135

Overflow

If operands are too big, then sum cannot be represented as an n-bit 2’s comp number.

01000 (8) 11000 (-8)

+ 01001 (9) + 10111 (-9)

10001 (-15) 01111 (+15)

CS 135

We have overflow if: signs of both operands are the same, and

sign of sum is different.

( ) ( )

Overflow

If we add two positive numbers and we get a carry into the sign bit we have a problem

3 0011 0100

CS 135

3 0011 4 0100

3 0011 4 0100

6 0110 8 1000

Page 18: Recall: what are Computers meant to do - George …bhagiweb/cs135/lectures... ·  · 2010-09-022 Problem Transformation - levels of abstraction Natural Language Algorithm Program

18

Overflow

If we add two positive numbers and we get a carry into the sign bit we have a problem

3 0011 4 0100

CS 135

3 0011 4 0100

6 0110 8 1000

carry in 0carry out 0

carry in 1carry out 0

Overflow

If we add two negative numbers and we get a carry into the sign bit do we always have a problem-5 1011 -4 1100

2 1110 1011

CS 135

-2 1110 -5 1011

-7 -9

Overflow

If we add two negative numbers and we get a carry into the sign bit do we have a problem-5 1011 -4 1100

-2 1110 -5 1011

CS 135

-7 11001 -9 10111

carry in 1carry out 1

carry in 0carry out 1

Overflow

If we add a positive and a negative number we won't have a problem

5 0101 -4 1100

-3 1101 5 0101

CS 135

3 1101 5 0101

2 10010 1 10001

carry in 1carry out 1

carry in 1carry out 1

Page 19: Recall: what are Computers meant to do - George …bhagiweb/cs135/lectures... ·  · 2010-09-022 Problem Transformation - levels of abstraction Natural Language Algorithm Program

19

Overflow

Carry out of the leading digit

If we add two positive numbers and we get a carry into the sign bit we have a problem

If we add two negative numbers and we

CS 135

gget a carry into the sign bit do we have a problem (??)

If we add a positive and a negative number we won't have a problem

Overflow Condition

In terms of Carry-in and Carry-out ??

If carry in is not equal to carry out of MSB then overflow – easy to check in H/W

CS 135

Multiplication

How do you multiply two decimal numbers: 25 X 32 = 25 X (3.101 + 2.100) 25 + 25X 101 X 3

Similar method for binary For every bit position in the multiplier, shift

multiplicand left, multiply by 1 or 0 and add to result 1101 X 1011 = (13 X 11)

CS 135

1101 X 1011 = (13 X 11)1101

11010000

1101

10001111 (143)

A note on computer arithmetic

What actual type of additions (arith operations) are taking place ? recall: max number T_max represented by N

bit unsigned integer is 2^N

If N=8 then max is 255

Modulo arithmetic: mod (T max +1)

CS 135

Modulo arithmetic: mod (T_max +1) If N=8, then (a+b) = (a+b) mod 256

Even bigger problem with multiplication 2N bit product, but truncated to N bits!

Is (a+b) + c = a + (b+c) ? Not always!! What if b,c are large

negative numbers- can have overflow

Page 20: Recall: what are Computers meant to do - George …bhagiweb/cs135/lectures... ·  · 2010-09-022 Problem Transformation - levels of abstraction Natural Language Algorithm Program

20

Logical Operators

CS 135

Another use for bits: Logic

Beyond numbers

logical variables can be true or false, on or off, etc., and so are readily represented by the binary system.

A logical variable A can take the values false = 0 or

CS 135

A logical variable A can take the values false 0 or true = 1 only. Logical Variables = Propositions in propositional logic

The manipulation of logical variables is known as Boolean Algebra, and has its own set of operations -which are not to be confused with the arithmetical operations of the previous section.

Some basic operations: NOT, AND, OR, XOR

Propositional Logic

each variable has True (T) or False (F) value

Use logical connectives to build more complex propositions (i.e., logic statements)

Connectives: AND, OR, NOT, …

(A AND B) is True if A is True and B is true….

Build “truth table” for propositional ‘formula’

CS 135

Build truth table for propositional formula

A B A AND BF F FF T FT F FT T T

A B A OR BF F FF T TT F TT T T

A NOT AF TT F

Boolean Logical Operations

Represent propositions using binary representationOperations on logical TRUE or FALSE variables

Boolean variables two states -- takes one bit to represent: TRUE=1, FALSE=0

A B A AND B0 0 0

A B A OR B0 0 0

A NOT A0 1

CS 135

View n-bit number as a collection of n logical values operation applied to each bit independently

0 1 01 0 01 1 1

0 1 11 0 11 1 1

1 0

Page 21: Recall: what are Computers meant to do - George …bhagiweb/cs135/lectures... ·  · 2010-09-022 Problem Transformation - levels of abstraction Natural Language Algorithm Program

21

A B A XOR B

0 0

0 1

CS 135

0 1

1 0

1 1

Logic Operations..more examples

A B C (A AND B) (NOT C) (A AND B) OR (NOT C)

0 0 0

0 0 1

CS 135

Basic Logic Operations

Equivalent Notations not A = A’ = A

A and B = A.B = AB = A intersection B

A or B = A+B = AB = A union B

Truth Tables of Basic Operations

CS 135

A or B A+B AB A union B

Other common logic operations: NAND = NOT AND

Find AND and then Complement it (invert bit)

NOR = NOT OR Find OR and then Complement it

XNOT = NOT XOR

Bitwise Logical Operators

View n-bit number as a collection of nlogical values operation applied to each bit independently

Number operated on is an n-bit number

Operation being performed is logical

CS 135

Operation being performed is logical operation on each bit

Page 22: Recall: what are Computers meant to do - George …bhagiweb/cs135/lectures... ·  · 2010-09-022 Problem Transformation - levels of abstraction Natural Language Algorithm Program

22

Bitwise AND

0101 AND 0111 in C:(5 & 6)

0101

0110

CS 135

0110

Why use bitwise operators?

Masking operations If we are only interested in last 8 bits of a 16

bit number X, how to extract this?

X & x00FF Zero out the most significant 8 bits; value of least significant 8

bits is same as the value of these in X

CS 135

x27A4 & x00FF = x00A4

Bitwise OR

0101 OR 0111 in C:(5 | 6)

0101

0110

CS 135

0110

Bitwise NOT (Complement)

NOT 0101 in C: ~5

0101

CS 135

Page 23: Recall: what are Computers meant to do - George …bhagiweb/cs135/lectures... ·  · 2010-09-022 Problem Transformation - levels of abstraction Natural Language Algorithm Program

23

Bitwise XOR

0101 XOR 0111 in C: 5^6

0101

0110

CS 135

0110

Bitwise NAND

0101 NAND 0111 No C Operator

~(5 & 6)

0101

0110

CS 135

0110

Bitwise NOR

0101 NOR 0111 No C Operator

~(5 | 6)

0101

0110

CS 135

0110

Boolean Relational Operators ?

What is the semantics of: If (x==0) then …… how many outcomes for (x==0) ?

Concept of boolean operators Apply logic operators, but treat input and output as

boolean variables

CS 135

Only 1 or 0 (True or False) values for entire variable

But input strings can be n-bits long? Treat entire string as ONE boolean variable How ?

Boolean operators Logical AND: (x AND y) =1 if both x and y are non-zero Logical OR: (x OR y)=1 if at least one of x,y are non-

zero Logical NOT: (NOT x)=1 if x=0 and (NOT x)=0 if x is

non-zero.

Page 24: Recall: what are Computers meant to do - George …bhagiweb/cs135/lectures... ·  · 2010-09-022 Problem Transformation - levels of abstraction Natural Language Algorithm Program

24

LC-3 Data Types

Some data types are supported directly by theinstruction set architecture.

For LC-3, there is only one hardware-supported data type:

16-bit 2’s complement signed integer

CS 135

16-bit 2 s complement signed integer

Operations: ADD, AND, NOT

Other data types are supported by interpreting16-bit values as logical, text, fixed-point, etc.,in the software that we write.

Next…a little bit of “reality”

look at how some of the concepts we have studied take shape in ‘real life’ C programming and O/S

CS 135

Data Representations

Sizes of C Objects (in Bytes) C Data Type Compaq Alpha Typical 32-bit Intel IA32

int 4 4 4

long int 8 4 4

char 1 1 1

short 2 2 2

float 4 4 4

double 8 8 8

CS 135

double 8 8 8

long double 8 8 10/12

char * 8 4 4 Or any other pointer

Logical Operations in C

C supports both bitwise and boolean logic operations x & y bitwise logic operation

x && y boolean operation: output is boolean value

What’s going on here? In boolean operation the result has to be TRUE (1) or

CS 135

In boolean operation the result has to be TRUE (1) or FALSE (0)

Treats any non-zero argument as TRUE and returns only TRUE (1) or FALSE (0)

In C: logical operators do not evaluate their second argument if result can be obtained from first a && 5/a can we get divide by zero error?

Page 25: Recall: what are Computers meant to do - George …bhagiweb/cs135/lectures... ·  · 2010-09-022 Problem Transformation - levels of abstraction Natural Language Algorithm Program

25

Bitwise Review

Can only be applied to integral operands

that is, char, short, int and long

(signed or unsigned)

& Bitwise AND

| Bitwise OR

CS 135

^ Bitwise XOR

<< Shift Left

>> Shift Right

~ 1’s Complement (Inversion)

Shift Operations

x >> y x right shifted y bit positions, sign extended

Sign bit shifted into positions vacated by shifted bits

x= 011000 y=2 x >> y = ? z = 101000 y=2

CS 135

z 101000 y 2 z >> y = ?

x << y x left shifted y bit positions, zero placed in

positions vacated by shifted bits x << y = ? z << y = ?

Logical Operations in C

! Logical NOT !x

!x=0 if x is non-zero, !x=1 if value of x is zero

&& Logical AND x && y

CS 135

x && y = 1 if value of x is not zero and value of y is not zero

x && y = 0 if both x and y are zero

|| logical OR x || y

x || y =1 if at least one of x,y are not zero

x || y = 0 if both x,y are zero

Examples

8 bit numbers, f=7, g=8 f= 00000111 g = 00001000

h= (f & g) (bitwise AND)…. h= ?

h = (f && g) (logical AND)…

CS 135

h = ?

h= (f | g) (bitwise OR)… h= ? h= (f || g) (logical OR)…. h= ? h= (~f | ~g)…h=? h= (!f && !g)…h=?

Page 26: Recall: what are Computers meant to do - George …bhagiweb/cs135/lectures... ·  · 2010-09-022 Problem Transformation - levels of abstraction Natural Language Algorithm Program

26

Unsigned & Signed Numeric Values

Equivalence Same encodings for

nonnegative values

Uniqueness Every bit pattern represents

unique integer value

Each representable integer

X B2T(X)B2U(X)0000 00001 10010 20011 30100 40101 50110 6

0123456

CS 135

Each representable integer has unique bit encoding

Can Invert Mappings U2B(x) = B2U-1(x)

Bit pattern for unsigned integer

T2B(x) = B2T-1(x) Bit pattern for two’s comp integer

60111 7

–88–79–610–511–412–313–214–115

10001001101010111100110111101111

67

Signed vs. Unsigned in CSigned vs. Unsigned in C

Constants By default are considered to be signed integers

Unsigned if have “U” as suffix0U, 4294967259U

Casting

CS 135

Explicit casting between signed & unsigned same as U2T and T2Uint tx, ty;

unsigned ux, uy;

tx = (int) ux;

uy = (unsigned) ty;

Implicit casting also occurs via assignments and procedure callstx = ux;

uy = ty;

short int x = 15213;unsigned short int ux = (unsigned short) x;short int y = -15213;unsigned short int uy = (unsigned short) y;

Casting Signed to Unsigned

C Allows Conversions from Signed to Unsigned

CS 135

Resulting Value No change in bit representation – only in interpretation

What is value of ux ?

What is value of uy?

Relation Between Signed & UnsignedWeight -15213 50323

1 1 1 1 12 1 2 1 24 0 0 0 08 0 0 0 0

16 1 16 1 1632 0 0 0 064 0 0 0 0

128 1 128 1 128256 0 0 0 0

CS 135

uy = y + 2 * 32768= y + 65536

256 0 0 0 0512 0 0 0 0

1024 1 1024 1 10242048 0 0 0 04096 0 0 0 08192 0 0 0 0

16384 1 16384 1 1638432768 1 -32768 1 32768

Sum -15213 50323

Page 27: Recall: what are Computers meant to do - George …bhagiweb/cs135/lectures... ·  · 2010-09-022 Problem Transformation - levels of abstraction Natural Language Algorithm Program

27

T2U

T2B B2U

Two’s Complement Unsigned

Maintain Same Bit Pattern

x uxX

Relation between Signed & Unsigned

CS 135

+ + + + + +• • •

- + + + + +• • •

ux

x-

w–1 0

+2w–1 – –2w–1 = 2*2w–1 = 2w

ux x x 0

x 2w x 0

short int x = 15213;unsigned short int ux = (unsigned short) x;short int y = -15213;unsigned short int uy = (unsigned short) y;

Casting Signed to Unsigned

C Allows Conversions from Signed to Unsigned

CS 135

Resulting Value No change in bit representation

Nonnegative values unchanged ux = 15213

Negative values change into (large) positive values uy = 50323

Signed vs. Unsigned in CSigned vs. Unsigned in C

Constants By default are considered to be signed integers

Unsigned if have “U” as suffix0U, 4294967259U

Casting

CS 135

Explicit casting between signed & unsigned same as U2T and T2Uint tx, ty;

unsigned ux, uy;

tx = (int) ux;

uy = (unsigned) ty;

Implicit casting also occurs via assignments and procedure callstx = ux;

uy = ty;

0 0U == unsigned

-1 0 < signed

-1 0U > unsigned

Casting SurprisesCasting Surprises Expression Evaluation

If mix unsigned and signed in single expression, signed values implicitly cast to unsigned

Including comparison operations <, >, ==, <=, >=

Examples for W = 32

Constant1 Constant2 Relation Evaluation0 0U

-1 0

-1 0U

CS 135

1 0U > unsigned

2147483647 -2147483648 > signed

2147483647U -2147483648 < unsigned

-1 -2 > signed

(unsigned) -1 -2 > unsigned

2147483647 2147483648U < unsigned

2147483647 (int) 2147483648U > signed

1 0U

2147483647 -2147483648

2147483647U -2147483648

-1 -2

(unsigned) -1 -2

2147483647 2147483648U

2147483647 (int) 2147483648U

Page 28: Recall: what are Computers meant to do - George …bhagiweb/cs135/lectures... ·  · 2010-09-022 Problem Transformation - levels of abstraction Natural Language Algorithm Program

28

Why Should I Use Unsigned?

Don’t Use Just Because Number Nonzero Easy to make mistakes

for (i = cnt-2; i >= 0; i--)

a[i] += a[i+1];

Do Use When Performing Modular Arithmetic Multiprecision arithmetic

CS 135

p

Other esoteric stuff

Do Use When Need Extra Bit’s Worth of Range Working right up to limit of word size

Byte-Oriented Memory Organization

Programs Refer to Virtual Addresses Conceptually very large array of bytes

Actually implemented with hierarchy of different memory types SRAM, DRAM, disk

Only allocate for regions actually used by program

In Unix and Windows NT address space private to

CS 135

In Unix and Windows NT, address space private to particular “process” Program being executed

Program can clobber its own data, but not that of others

Compiler + Run-Time System Control Allocation Where different program objects should be stored

Multiple mechanisms: static, stack, and heap

In any case, all allocation within single virtual address space

Encoding Byte Values

Byte = 8 bits Binary 000000002 to 111111112

Decimal: 010 to 25510

Hexadecimal 0016 to FF16 Base 16 number representation

U h t ‘0’ t ‘9’ d ‘A’ t ‘F’

0 0 00001 1 00012 2 00103 3 00114 4 01005 5 01016 6 0110

CS 135

Use characters ‘0’ to ‘9’ and ‘A’ to ‘F’

Write FA1D37B16 in C as 0xFA1D37B Or 0xfa1d37b

6 6 01107 7 01118 8 10009 9 1001A 10 1010B 11 1011C 12 1100D 13 1101E 14 1110F 15 1111

Machine Words

Machine Has “Word Size” Nominal size of integer-valued data

Including addresses

Most current machines are 32 bits (4 bytes) Limits addresses to 4GB

Becoming too small for memory-intensive applications

CS 135

g y pp

High-end systems are 64 bits (8 bytes) Potentially address 1.8 X 1019 bytes

Machines support multiple data formats Fractions or multiples of word size

Always integral number of bytes

Page 29: Recall: what are Computers meant to do - George …bhagiweb/cs135/lectures... ·  · 2010-09-022 Problem Transformation - levels of abstraction Natural Language Algorithm Program

29

Data Representations

Sizes of C Objects (in Bytes) C Data Type Compaq Alpha Typical 32-bit Intel IA32

int 4 4 4

long int 8 4 4

char 1 1 1

short 2 2 2

float 4 4 4

double 8 8 8

CS 135

double 8 8 8

long double 8 8 10/12

char * 8 4 4 Or any other pointer

Word-Oriented Memory Organization

Addresses Specify Byte Locations Address of first byte

in word

000000010002000300040005

32-bitWords

Bytes Addr.64-bitWords

Addr =??

Addr =??

Addr =

0000

0000

CS 135

in word

Addresses of successive words differ by 4 (32-bit) or 8 (64-bit)

0006000700080009001000110012001300140015

Addr =??

??

Addr =??

Addr =??

0004

0008

0012

0008

Byte Ordering

How should bytes within multi-byte word be ordered in memory?

Conventions Sun’s, PowerPC (old Mac’s) are “Big Endian”

machines

CS 135

Least significant byte has highest address

Alphas, PC’s are “Little Endian” machines Least significant byte has lowest address

Byte Ordering Example

Big Endian Least significant byte has highest address

Little Endian Least significant byte has lowest address

Example Variable has 4 byte representation 0 01234567

CS 135

Variable x has 4-byte representation 0x01234567

Address given by &x is 0x100

0x100 0x101 0x102 0x103

01 23 45 67

0x100 0x101 0x102 0x103

67 45 23 01

Big Endian

Little Endian

01 23 45 67

67 45 23 01

Page 30: Recall: what are Computers meant to do - George …bhagiweb/cs135/lectures... ·  · 2010-09-022 Problem Transformation - levels of abstraction Natural Language Algorithm Program

30

Representing Integers

int A = 15213; int B = -15213; long int C = 15213;

Decimal: 15213

Binary: 0011 1011 0110 1101

Hex: 0000 3 B 6 D

Decimal: -15213

Hex: FFFF C 4 9 3

CS 135

Little endian layout for A:

For B

For C

Big endian layout for A:

For B:

For C:

Representing Integers

int A = 15213; int B = -15213; long int C = 15213;

Decimal: 15213

Binary: 0011 1011 0110 1101

Hex: 3 B 6 D

6D3B

Linux/Alpha A

0000

Sun A

6D3B

Alpha C

0000

Sun C

6D3B

Linux C

CS 135

0000

3B6D

93C4FFFF

Linux/Alpha B

C493

FFFF

Sun B

Two’s complement representation(Covered next lecture)

00000000

3B0000

3B6D

003B0000

What next..

The hardware building blocks and their operations – Chapter 3

Digital Logic structures Basic device operations: CMOS transistor

Combinational Logic circuits

CS 135

g Gates (NAND, OR, NOT), Decoder, Multiplexer

Adders, multipliers

Sequential circuits– concept of memory Finite state machines, memory organization

Basic storage elements: latches, flip-flops

What next….reading

Read floating point representation

Quiz 1 on Thursday

HW1 posted

In-lab exercises this week

Ch k t b hi

CS 135

Check your team memberships

Will be posting team discussion problems by Thursday 6pm Come prepared after team discussions

Read Chapter 3

Page 31: Recall: what are Computers meant to do - George …bhagiweb/cs135/lectures... ·  · 2010-09-022 Problem Transformation - levels of abstraction Natural Language Algorithm Program

31

Limitations of integer representationsMost numbers are not integer!

Even with integers, there are other considerations

Range:The magnitude of the numbers we can represent is

determined by how many bits we use:

CS 135

y y e.g. with 32 bits the largest number we can represent is about +/- 2 billion, far

too small for many purposes.

Precision:The exactness with which we can specify a number:

e.g. a 32 bit number gives us 31 bits of precision, or roughly 9 figure precision in decimal repesentation.

We need other data types!

Some History…

The Indiana Legislature once proposed legislation declaring that the value of was exactly 3.2

CS 135

Real numbers

Our decimal system handles non-integer realnumbers by adding yet another symbol - the decimal point (.) to make a fixed point notation:e.g. 3,456.78 = 3.103 + 5.102 + 4.101 + 6.100 + 7.10-1 +

8.10-2

CS 135

How can we represent fractions?Use a “binary point” to separate positive

from negative powers of two -- just like “decimal point.”

Real Numbers and Fractions in Binary

How to represent 6 5/8 in binary

6 5/8 = 4 + 2 + 1/2 + 1/8

6 5/8 = (1.22 + 1. 21 + 0. 20 + 1. 2-1 + 0. 2-2

+ 1. 2-3)

6 5/8 = 110 101

CS 135

6 5/8 = 110.101

Page 32: Recall: what are Computers meant to do - George …bhagiweb/cs135/lectures... ·  · 2010-09-022 Problem Transformation - levels of abstraction Natural Language Algorithm Program

32

Fractions: Fixed-Point

2’s comp addition and subtraction still work. if binary points are aligned

2-1 = 0.5

CS 135

00101000.101 (40.625)

+ 11111110.110 (-1.25)

00100111.011 (39.375)

2-2 = 0.25

2-3 = 0.125

No new operations -- same as integer arithmetic.

Do we still have problems ?

What about representing very large and very small numbers ? Using N bits, we can only represent x where -

-2^N <= x <= 2^N -1

The floating point, or scientific, notation allows us to represent very large and

CS 135

p y gvery small numbers (integer or real), with as much or as little precision as needed: Unit of electric charge e = 1.602 176 462 x 10-

19 Coul. Volume of universe = 1 x 1085 cm3

the two components of these numbers are called the mantissa and the exponent

Real numbers in binary

We mimic the decimal floating point notation to create a “hybrid” binary floating point number:We first use a “binary point” to separate whole

numbers from fractional numbers to make a fixed point notation: e.g. 00011001.110 = 1.24 + 1.103 + 1.101 + 1.2-1 + 1.2-2 => 25.75

CS 135

g(2-1 = 0.5 and 2-2 = 0.25, etc.)

We then “float” the binary point: 00011001.110 => 1.1001110 x 24

mantissa = 1.1001110, exponent = 4

Now we have to express this without the extra symbols ( x, 2, . ) by convention, we divide the available bits into three fields:

sign, mantissa, exponent

Scientific Notation

-6.023 x 10-23

Sign ofExponent

CS 135

Sign

NormalizedMantissa

Base

Exponent

??

Page 33: Recall: what are Computers meant to do - George …bhagiweb/cs135/lectures... ·  · 2010-09-022 Problem Transformation - levels of abstraction Natural Language Algorithm Program

33

Binary Floating Point Representation

Same basic idea as scientific notation

Modifications and improvments based on Hardware architecture

Efficiency (Space & Time)

Additional requirements

CS 135

Additional requirements Infinity

Not a number

Not normalized

etc.

IEEE-754

0 00000000 00000000000000000000000

sig

exponent mantissa (significand)

31 30 23 22 0

CS 135

n (-1)S * 1.M * 2 E-127

Sign

1 is understood

Mantissa (w/o leading 1)

Base

Biased Exponent

IEEE-754

0 00000000 00000000000000000000000

sig

exponent mantissa (significand)

CS 135

n (-1)S * 1.M * 2 E-127

Can any of these equal 0?Can any of these equal 0?

IEEE-754 fp numbers - 2

Example:25.75 => 00011001.110 => 1.1001110 x 24

sign bit = 0 (+ve)

normalized mantissa (fraction) = 100 1110 0000 0000 0000 0000

biased exponent = 4 + 127 = 131 => 1000 0011

25 75 0 1000 0011 100 1110 0000 0000 0000 0000 41CE0000

CS 135

so 25.75 => 0 1000 0011 100 1110 0000 0000 0000 0000 => x41CE0000

Values represented by convention:

Infinity (+ and -): exponent = 255 (1111 1111) and fraction

= 0

NaN (not a number): exponent = 255 and fraction 0

Zero (0): exponent = 0 and fraction = 0note: exponent = 0 => fraction is de-normalized, i.e no hidden 1

Page 34: Recall: what are Computers meant to do - George …bhagiweb/cs135/lectures... ·  · 2010-09-022 Problem Transformation - levels of abstraction Natural Language Algorithm Program

34

So how can we represent 0?

0 00000000 00000000000000000000000

sign

exponent mantissa (significand)

(-1)S * 1.M * 2 E-127

CS 135

n

Non-normalizedtypically

underflow

OrdinaryOld

Numbers

0Powers

ofTwo

NotA

Number

E == 0 0 < E < 255 E == 255

M==0

M!=0

Can be written...

0 00000000 00000000000000000000000

sign

exponent mantissa (significand)

(-1)S * 2 E-127 * 1.M

CS 135

n

Non-normalizedtypically

underflow

OrdinaryOld

Numbers

0Powers

ofTwo

E == 0 0 < E < 255 E == 255

M==0

M!=0NotA

Number

Pop Quiz!!!Pop Quiz!!! 0 00000000 00000000000000000000000

sign

exponent mantissa (significand)

(-1)S * 2 E-127 * 1.M

0 00000000 00000000000000000000000

1 00000000 00000000000000000000000

= 0

= -0

CS 135

n

Non-normalizedtypically

underflow

OrdinaryOld

Numbers

0Powers

ofTwo

E == 0 0 < E < 255 E == 255

M==0

M!=0NotA

Number

Page 35: Recall: what are Computers meant to do - George …bhagiweb/cs135/lectures... ·  · 2010-09-022 Problem Transformation - levels of abstraction Natural Language Algorithm Program

35

0 00000000 00000000000000000000000

sign

exponent mantissa (significand)

(-1)S * 2 E-127 * 1.M

0 11111111 00000000000000000000000

1 11111111 00000000000000000000000

= Infinity

= -Infinity

CS 135

n

Non-normalizedtypically

underflow

OrdinaryOld

Numbers

0Powers

ofTwo

E == 0 0 < E < 255 E == 255

M==0

M!=0NotA

Number

0 00000000 00000000000000000000000

sign

exponent mantissa (significand)

(-1)S * 2 E-127 * 1.M

0 11111111 00000100000000000000000

1 11111111 00100010001001010101010

= NaN

= NaN

CS 135

n

Non-normalizedtypically

underflow

OrdinaryOld

Numbers

0Powers

ofTwo

E == 0 0 < E < 255 E == 255

M==0

M!=0NotA

Number

0 00000000 00000000000000000000000

sign

exponent mantissa (significand)

(-1)S * 2 E-127 * 1.M

0 10000000 00000000000000000000000 = +1 * 2(128-127) * 1.0 = 2

CS 135

n

Non-normalizedtypically

underflow

OrdinaryOld

Numbers

0Powers

ofTwo

E == 0 0 < E < 255 E == 255

M==0

M!=0NotA

Number

0 00000000 00000000000000000000000

sign

exponent mantissa (significand)

(-1)S * 2 E-127 * 1.M

0 10000001 10100000000000000000000

1 10000001 10100000000000000000000

= +1 * 2(129-127) * 1.101

= -1 * 2(129-127) * 1.101

= 6.5

= -6.5

CS 135

n

Non-normalizedtypically

underflow

OrdinaryOld

Numbers

0Powers

ofTwo

E == 0 0 < E < 255 E == 255

M==0

M!=0NotA

Number

Page 36: Recall: what are Computers meant to do - George …bhagiweb/cs135/lectures... ·  · 2010-09-022 Problem Transformation - levels of abstraction Natural Language Algorithm Program

36

0 00000000 00000000000000000000000

sign

exponent mantissa (significand)

(-1)S * 2 E-127 * 1.M

0 00000001 00000000000000000000000

0 00000000 10000000000000000000000

= +1 * 2(1-127) * 1.0

= +1 * 2(-126) * 0.1

= 2(-126)

= 2(-127)

CS 135

n

Non-normalizedtypically

underflow

OrdinaryOld

Numbers

0Powers

ofTwo

E == 0 0 < E < 255 E == 255

M==0

M!=0NotA

Number

Even Larger Numbers?IEEE-754 fp double precision

Double precision (64 bit) floating point

s biased exp. fraction

1 11 bits 52 bits

N = (-1)s x 1.fraction x 2(biased exp. – 1023)

64 bits:

CS 135

Range & Precision: 32 bit: mantissa of 23 bits + 1 => approx. 7 digits decimal 2+/-127 => approx. 10+/-38

64 bit: mantissa of 52 bits + 1 => approx. 15 digits decimal 2+/-1023 => approx. 10+/-306