COMPUTER SCIENCE Data Representation and Machine Concepts Section 1.6 Instructor: Lin Chen Sept...

13
COMPUTER SCIENCE Data Representation and Machine Concepts Section 1.6 Instructor: Lin Chen Sept 2013

Transcript of COMPUTER SCIENCE Data Representation and Machine Concepts Section 1.6 Instructor: Lin Chen Sept...

Page 1: COMPUTER SCIENCE Data Representation and Machine Concepts Section 1.6 Instructor: Lin Chen Sept 2013.

COMPUTER SCIENCEData Representation and Machine ConceptsSection 1.6

Instructor: Lin Chen

Sept 2013

Page 2: COMPUTER SCIENCE Data Representation and Machine Concepts Section 1.6 Instructor: Lin Chen Sept 2013.

1.6 Storing Integers

1 Data Storage

Two’s Complement

Excess Notation

Pearson Custom: Computer Science, Data Representation and Machine Concepts

http://ocw.mit.edu/courses/aeronautics-and-astronautics/16-01-unified-engineering-i-ii-iii-iv-fall-2005-spring-2006/comps-programming/number_systems.pdf

Page 3: COMPUTER SCIENCE Data Representation and Machine Concepts Section 1.6 Instructor: Lin Chen Sept 2013.

Two’s Complement

1.6 Storing Integers

Use a fixed number of bits to represent each of the values in the system

First bit is used to represent the sign, 1 represents negative number, 0 represent nonnegative number

A negative number is represented by inverting the bits of its corresponding positive number in each place, then add 1 to the inverted number

Suppose to use 4 bits to represent the number

01112 = 7 10

10002

10012 = -7 10

+1

Page 4: COMPUTER SCIENCE Data Representation and Machine Concepts Section 1.6 Instructor: Lin Chen Sept 2013.

Two’s Complement

1.6 Storing Integers

Write the two’s complement notation for -6, -3, -1.

Page 5: COMPUTER SCIENCE Data Representation and Machine Concepts Section 1.6 Instructor: Lin Chen Sept 2013.

Two’s Complement

1.6 Storing Integers

Bit Pattern

Value Represented

0111 70110 60101 50100 40011 30010 20001 10000 01111 -11110 -21101 -31100 -41011 -51010 -61001 -71000 -8

Four Bits

Page 6: COMPUTER SCIENCE Data Representation and Machine Concepts Section 1.6 Instructor: Lin Chen Sept 2013.

Two’s Complement

1.6 Storing Integers

Addition7 – 2 = 7 + (-2) = ?

01111110+

10101

Page 7: COMPUTER SCIENCE Data Representation and Machine Concepts Section 1.6 Instructor: Lin Chen Sept 2013.

Two’s Complement

1.6 Storing Integers

Addition7 – 2 = 7 + (-2) = ?

01111110+

10101

Use four bits

01012 = 510

Page 8: COMPUTER SCIENCE Data Representation and Machine Concepts Section 1.6 Instructor: Lin Chen Sept 2013.

Two’s Complement

1.6 Storing Integers

Use two’s complement notation to calculate:7 – 5 = ?-3 – 4 = ?6 – 4 = ?-2 – 5 = ?

Page 9: COMPUTER SCIENCE Data Representation and Machine Concepts Section 1.6 Instructor: Lin Chen Sept 2013.

Two’s Complement

1.6 Storing Integers

What is the range of numbers that can be represented using the 2’s complement?

Answer: 2N-1 – 1 to -2N-1

Three bits: 3 to -4

Four bits: 7 to -8

Eight bits: ?

N is the number of bits

Page 10: COMPUTER SCIENCE Data Representation and Machine Concepts Section 1.6 Instructor: Lin Chen Sept 2013.

Two’s Complement

1.6 Storing Integers

List all the number with the two’s complement notation using 3 bits

Express 16 - -16 with the two’s complement notation using 8 bits

Page 11: COMPUTER SCIENCE Data Representation and Machine Concepts Section 1.6 Instructor: Lin Chen Sept 2013.

Two’s Complement

1.6 Storing Integers

Overflow

Calculate 5 + 4 with two’s complement notation using 4 bits

Page 12: COMPUTER SCIENCE Data Representation and Machine Concepts Section 1.6 Instructor: Lin Chen Sept 2013.

Excess Notation

1.6 Storing Integers

Steps:

1. Add the excess value 2N-1 to the number, where N is the number of bits used

2. Convert the resulting number into binary format

Suppose use 4 bits

-6 + 8

24-1

= 2 0010

Page 13: COMPUTER SCIENCE Data Representation and Machine Concepts Section 1.6 Instructor: Lin Chen Sept 2013.

Two’s Complement

1.6 Storing Integers

Write -8 to 7 with excess notation with 4 bits

Write -4 to 3 with excess notation with 3 bits