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

## Presentation on theme: "15 October 2013Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems."— Presentation transcript:

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

Recap: Programs A program is a sequence of instructions. The instructions may refer to memory cells which store values such as integers. In Toms computer the memory cells are the boxes. Each memory cell has an address and contents. 15 October 2013Birkbeck College, U. London2

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 2013Birkbeck College, U. London3

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 2013Birkbeck College, U. London4

15 October 2013Birkbeck College, U. London5 Exercise from Week 2 Sketch an algorithm that takes as input a strictly positive integer n and outputs an integer k such that

15 October 2013Brookshear, Section 1.66 Representations of Negative Integers Put a minus sign in front of the representation for a positive integer. Excess notation. Twos Complement notation – the most popular representation for negative integers in computers.

15 October 2013Brookshear, Section 1.67 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.

15 October 2013Brookshear, Section 1.68 Example of Excess Notation n=3 111 3 110 2 101 1 100 0 011 -1 010 -2 001 -3 000 -4

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 2013Birkbeck College, U. London9

15 October 2013Birkbeck College, U. London10 Twos 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.

15 October 2013Brookshear, Section 1.611 Example of Twos Complement Notation 0111 7 0110 6 0101 5 0100 4 0011 3 0010 2 0001 1 0000 0 1111 -1 1110 -2 1101 -3 1100 -4 1011 -5 1010 -6 1001 -7 1000 -8 n=4 The left most bit indicates the sign.

15 October 2013Brookshear, Section 1.612 Addition and Subtraction In the twos complement system subtraction reduces to addition. E.g. to evaluate 6-5 in 4 bit twos complement notation, add the tc bit strings for 6 and –5, then take the four rightmost bits. 0110 6 1011 -5 === == 10001 1

15 October 2013Brookshear, Section 1.613 Explanation The bit strings for TC[6] and TC[-5] are the rightmost four bits of Binary[2 4 +6] and Binary[2 4 -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[(2 4 +6)+(2 4 -5)]= Binary[2 4 +2 4 +1]. The right most four bits of Binary[2 4 +2 4 +1] are the bit string for TC[1].

Why Use Twos 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 2013Brookshear, Section 1.614

15 October 2013Brookshear, Section 1.615 Twos 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.

15 October 2013Birkbeck College, U. London16 Example Find the 5 bit twos complement representations for the decimal integers 5 and -5.

Similar presentations