Title: Computer Systems and Elements of Programming
1Computer Systems and Elements of Programming
- Lecturer Steve Maybank
- School of Computer Science and Information
Systems - sjmaybank_at_dcs.bbk.ac.uk
- http//www.dcs.bbk.ac.uk/sjmaybank
- Autumn 2009
- Week 5a Number Representations
2Algorithm for Computing the GCD
- Define Floorxlargest integer n such that nltx,
where x is a real number. - Input integers m, n such that mgtngt0 and mgt0
- While n?0
- qFloorm/n
- rm-qn
- mn
- nr
- EndWhile
- Printm
3Integers
- The integers are the whole numbers ,
-2,-1,0,1,2, - Natural numbers 0,1,2,3
- Strictly positive integers 1,2,3,
- Strictly negative integers -1,-2,-3
- If a, b are integers then exactly one of the
following statements is true - altb, a b, a gt b
4Fractions
- Fractions are ratios of integers, e.g. 1/2,
10/36,-4/5, 0/1, - In the ratio a/b, a is the numerator and b is the
denominator. - The denominator cannot be zero.
- If rltgt0, then the number (r a)/(r b) is the same
as the number a/b. - The fractions are called rational numbers.
5Arithmetic with Fractions
6Examples
- 7/121/3
- 3/72/11
- 4/9-1/7
- Find the larger of the following two fractions
2/7, 3/8. - Find a fraction of the form a/7 that is as close
as possible to 1/3.
7Decimal Fractions
- The fraction a/b is a decimal fraction if b is a
power of 10, b10n where n is an integer. - Examples 1/10, 16/100, 283/1000.
- Notation 0.1, 0.16, 0.283.
- The point in 0.16 is called the radix point.
8Dyadic Numbers
- The fraction a/b is a dyadic number if b is a
power of 2, b2n where n is an integer. - Examples ½, 16/26 , 47/27.
- Binary notation 0.1, 0.01, 0.0101111.
- The point in 0.1 is called the radix point.
9Binary Fractions and Decimal Fractions
- BinaryFraction101.01
- BinaryFraction10101/100
- DecimalFraction21/4
- DecimalFraction5 ¼
- BinaryFraction101.01
- DecimalFraction1x22 0x21 1x20 0x2-1 1x2-2
- DecimalFraction411/4
- DecimalFraction5 ¼
10Example Addition of Binary Fractions
carry
carry
carry
carry
11Example Subtraction of Binary Integers
Repay in 2nd row
Borrow in top row
12Examples
- Add the following two binary fractions 101.1,
1.11 - Subtract the binary integer 1011 from the binary
integer 11001.
13Representations 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.
14Excess Notation
- Problem represent a set of positive and negative
integers using bit strings with a fixed length n. - Represent 0 by 100 (n bits).
- Represent positive numbers by counting up from
100 in standard binary notation. - Represent negative integers by counting down from
100 in standard binary notation.
15Example of Excess Notation
111 3 110 2 101 1 100 0 011
-1 010 -2 001 -3 000 -4
16Twos Complement Notation
- Form the bit string 100 with n1 bits.
- Represent 0 by the last n bits of 100.
- Represent positive integers by counting up from
100 in standard binary notation and using the
last n bits. - Represent negative integers by counting down from
100 in standard binary notation and using the
last n bits.
17Example 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
n4 The left most bit indicates the sign.
18Addition 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
19Explanation
- The bit string for TC6 equals the rightmost
four bits of the bit string for Binary246,
etc. - The bit strings TC6, TC-5 are added as if
they were binary numbers. The rightmost four bits
of the result equal the rightmost four bits of - Binary(246)(24-5) Binary2424 1.
- The right most four bits of Binary2424 1
equal the bit string for TC1.
20Twos Complement Notation for m and -m
- Suppose TCm s 1 t, where t is a string
of zeros. - Then TC-mComplements1t.
- Proof the rightmost n bits of TCmTC-m are
all zero. - Example n4,
- TC30011, TC-31101.
21Example
- Find the 5 bit twos complement representations
for 5 and -5.