Transcript week 2

Number Representation 1
Lecture 2
Outcomes

By the end of the session you should:
– Understand what bits/bytes/words are.
– Understanding conversion between binary, decimal
and hexadecimal.
– Understand how negative numbers can be stored.
Numbering Systems (Decimal)

The counting system most commonly used by humans
in the west, often believed to be based on the number
of fingers on two hands. There are ten digits in this
counting system
Numbering Systems (Binary)





The two-state nature of logic gates means the use of 0
or 1, as the basic unit of the count is natural.
Data is represented by binary digits (bits),
words are groups of bits, but by convention the size of
words are multiples of 8 bits (or a byte).
bit furthest right as the least significant bit (lsb) and
bit furthest left as the most significant bit as the most
significant bit (msb).


Where we are only dealing with the positive integers
the msb is the representation of largest power of 2,
this as we will see later is not always true.
In an n-bit word there are 2n different combinations of
bit,
– so for 2 bits(n=2) there are 4 combinations,
– for n=5 32 combinations.
Decimal
msb
Lsb
128
64
32
16
8
4
2
1
255
1
1
1
1
1
1
1
1
8
0
0
0
0
1
0
0
0
33
0
0
1
0
0
0
0
1
99
0
1
1
0
0
0
1
1
46
0
0
1
0
1
1
1
0
Numbering system (Hexadecimal)

A base-16 system with 16 possible digits
{0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F}. Each hexadecimal
number can be represented by 4 bits .
Hexadecimal
0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F
Binary
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
Decimal
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Negative and Positive Numbers


So far is the discussion no mention has been made
about the being able to represent negative numbers,
how can both negative and positive number be stored.
Two methods
– Sign and Magnitude
– 2’s complement.
2’s complement


There is an alternative, which allows addition and
subtraction to be treated in the same way. 2’s
complement has the ‘sign’ of the number built in. This
achieved by the most significant bit the value –2n-1
having a negative value
so if n=8 this is –128 and the rest of the bits are
unsigned bits.
2’s complement


If 10000001 was stored the msb =-128 and the rest
equals 1 so the number is –128+1=-127.
If 00000001 was stored the msb =0 and the rest
equals 1 so the number this time is 0+1=1
-128
10000000
-127
10000001
-126
10000010
:
::::::::
-3
11111101
-2
11111110
-1
11111111
0
00000000
+1
00000001
+2
00000010
+3
00000011
:
:::::::
+124
01111100
+125
01111101
+126
01111110
+127
01111111




So with 8-bit 2’s complement we can represent
numbers between –128 and +127.
-126
=
10000010
+126
=
01111110
If we reverse all the bits in –126 we get
01111101 if we add 1 to this we get
01111110
Positive to negative and back
Starting Number = -126
10000010
Reverse bits
01111101
Add 1
01111110
+126
01111110
Summary

Introduced
–
–

Binary
Hexadecimal
Storing negative and positive numbers
–
–
Sign and magnitude
2’s complement.
Practical this week and Next Weeks
sessions

This weeks practical
–
–

Number representation
Adding binary numbers
Next Week
–
–
Subtracting in binary
How do we represent number such as 12.56 in a
computer?
Further reading

Chalk et al (2004) pg 10-19

Tannebaum (2005) Pages 69-70, 679-690