Introduction to Computer Systems Lecturer: Steve Maybank Week 3a: Number Representations

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 2014
Week 3a: Number Representations
14 October 2014
Birkbeck College, U. London
1
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.
14 October 2014
Birkbeck College, U. London
2
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 selected memory cells.
14 October 2014
Birkbeck College, U. London
3
Recap: Variables

Here are two typical statements in a programming language:
p = 0;
q = 3+4;

Left hand side: the name of a variable

Right hand side: an expression

Execution: evaluate the right hand side to obtain a number.
Store the number in a memory location named by the variable.
14 October 2014
Birkbeck College, U. London
4
Exercise from Week 2

Sketch an algorithm that takes as input a
strictly positive integer n and outputs an
integer k such that
2  n and 2
k
14 October 2014
k 1
n
Birkbeck College, U. London
5
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 integers in
computers.
14 October 2014
Brookshear, Section 1.6
6
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.
14 October 2014
Brookshear, Section 1.6
7
Example of Excess Notation
n=3
14 October 2014
111
110
101
3
2
1
100
0
011
010
001
000
-1
-2
-3
-4
Brookshear, Section 1.6
8
Examples


Find the 6 bit excess notation for the
decimal numbers 7 and -6.
Which decimal number has the 5 bit
excess notation 10101.
14 October 2014
Birkbeck College, U. London
9
Two’s Complement Notation

Form the bit string 10…0 with n+1 bits.

Represent 0 by the rightmost n bits of 10…0.


Represent positive integers by counting up from
10…0 in standard binary notation and using the
rightmost n bits.
Represent negative integers by counting down from
10…0 in standard binary notation and using the
rightmost n bits.
14 October 2014
Birkbeck College, U. London
10
Examples
Four bit two's complement representation for  5
2 4  5  16  5  11
Decimal 11  Binary1011
Answer :1011
Four bit two's complement representation for 6
2 4  6  16  6  22
Decimal 22  Binary10110
Answer : 0110
14 October 2014
Birkbeck College, U. London
11
Example of Two’s Complement Notation
0111
0110
0101
0100
0011
0010
0001
0000
7
6
5
4
3
2
1
0
1111
1110
1101
1100
1011
1010
1001
1000
-1
-2
-3
-4
-5
-6
-7
-8
n=4
The left most bit indicates the sign.
14 October 2014
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
1011
===
10001
14 October 2014
6
-5
==
1
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].
14 October 2014
Brookshear, Section 1.6
14
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.
14 October 2014
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.
14 October 2014
Brookshear, Section 1.6
16
Example

Find the 5 bit two’s complement
representations for the decimal integers
5 and -5.
14 October 2014
Birkbeck College, U. London
17