15 october 2013birkbeck college, u. london1 introduction to computer systems lecturer: steve maybank...

16
15 October 2013 Birkbeck College, U. London 1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems [email protected] http://www.dcs.bbk.ac.uk/~sjmaybank Autumn 2013 Week 3a: Number Representations

Upload: michael-warren

Post on 28-Mar-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 15 October 2013Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems

15 October 2013 Birkbeck College, U. London 1

Introduction to Computer Systems

Lecturer: Steve Maybank

Department of Computer Science and Information Systems

[email protected]://www.dcs.bbk.ac.uk/~sjmaybank

Autumn 2013

Week 3a: Number Representations

Page 2: 15 October 2013Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems

Recap: Programs A program is a sequence of instructions.

The instructions may refer to memory cells which store values such as integers.

In Tom’s computer the memory cells are the boxes.

Each memory cell has an address and contents.

15 October 2013 Birkbeck College, U. London 2

Page 3: 15 October 2013Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems

Recap: Running a Program The instructions of the program are

executed one by one.

When an instruction is executed, the values in the memory cells may change.

When the program halts the output is usually the values of one or more memory cells.

15 October 2013 Birkbeck College, U. London 3

Page 4: 15 October 2013Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems

Recap: Variables Here is a typical statement in a programming

language:q=0;

Left hand side: the name q of a variable

Right hand side: an expression

Execution: evaluate the right hand side to obtain a number. Store the number in a box named q.

15 October 2013 Birkbeck College, U. London 4

Page 5: 15 October 2013Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems

15 October 2013 Birkbeck College, U. London 5

Exercise from Week 2

Sketch an algorithm that takes as input a strictly positive integer n and outputs an integer k such that

nn kk 12 and 2

Page 6: 15 October 2013Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems

15 October 2013 Brookshear, Section 1.6 6

Representations of Negative Integers

Put a minus sign in front of the representation for a positive integer.

Excess notation.

Two’s Complement notation – the most popular representation for negative integers in computers.

Page 7: 15 October 2013Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems

15 October 2013 Brookshear, Section 1.6 7

Excess Notation Problem: represent a set of positive and

negative integers using bit strings with a fixed length n.

Represent 0 by 10…0 (n bits). Represent positive numbers by counting

up from 10…0 in standard binary notation. Represent negative integers by counting

down from 10…0 in standard binary notation.

Page 8: 15 October 2013Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems

15 October 2013 Brookshear, Section 1.6 8

Example of Excess Notation

n=3

111 3110 2101 1

100 0

011 -1010 -2001 -3000 -4

Page 9: 15 October 2013Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems

Examples Find the 6 bit excess notation for

the decimal numbers 7 and -6.

Which decimal number has the 5 bit excess notation 10101.

15 October 2013 Birkbeck College, U. London 9

Page 10: 15 October 2013Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems

15 October 2013 Birkbeck College, U. London 10

Two’s Complement Notation

Form the bit string 10…0 with n+1 bits. Represent 0 by the last n bits of 10…0. Represent positive integers by counting

up from 10…0 in standard binary notation and using the last n bits.

Represent negative integers by counting down from 10…0 in standard binary notation and using the last n bits.

Page 11: 15 October 2013Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems

15 October 2013 Brookshear, Section 1.6 11

Example of Two’s Complement Notation

0111 70110 60101 50100 40011 30010 20001 10000 0

1111 -11110 -21101 -31100 -41011 -51010 -61001 -71000 -8

n=4The left most bit indicates the sign.

Page 12: 15 October 2013Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems

15 October 2013 Brookshear, Section 1.6 12

Addition and Subtraction In the two’s complement system

subtraction reduces to addition. E.g. to evaluate 6-5 in 4 bit two’s

complement notation, add the tc bit strings for 6 and –5, then take the four rightmost bits.

0110 6 1011 -5 === ==10001 1

Page 13: 15 October 2013Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems

15 October 2013 Brookshear, Section 1.6 13

Explanation The bit strings for TC[6] and TC[-5] are

the rightmost four bits of Binary[24+6] and Binary[24 -5], respectively.

The bit strings TC[6], TC[-5] are added as if they were binary numbers. The rightmost four bits of the result equal the rightmost four bits of Binary[(24+6)+(24-5)]= Binary[24+24 +1].

The right most four bits of Binary[24+24 +1] are the bit string for TC[1].

Page 14: 15 October 2013Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems

Why Use Two’s Complement Addition and subtraction require

one circuit for addition and one circuit for negation.

This is more efficient than having a circuit for addition and a circuit for subtraction.

15 October 2013 Brookshear, Section 1.6 14

Page 15: 15 October 2013Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems

15 October 2013 Brookshear, Section 1.6 15

Two’s Complement Notation for m and -m

Suppose TC[m] = s || 1 || t, where t is a string of zeros.

Then TC[-m]=Complement[s]||1||t. Proof: the rightmost n bits of TC[m]

+TC[-m] are all zero. Example: n=4,

TC[3]=0011, TC[-3]=1101.

Page 16: 15 October 2013Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems

15 October 2013 Birkbeck College, U. London 16

Example

Find the 5 bit two’s complement representations for the decimal integers 5 and -5.