Sei sulla pagina 1di 22

Housekeeping

Remember support classes begin next week.

Assignment 1 is now available and is due at the beginning of your


support class in week 3 (1620 March).

An additional extended Euclidean algorithm example is up on moodle.

Tutorial Sheet 1 is available.

MAT1830

Lecture 3: Decimal and Other Notations

Example What does 4062 mean?

thousands
4
4062
4062

=
=

4 1000
4 103

+
+

hundreds
0
0 100
0 102

+
+

tens
6

ones
2

6 10
6 10

+
+

21
2 100

Example Write 501673 as a sum of powers of ten.


501673 = 5 105 + 0 104 + 1 103 + 6 102 + 7 10 + 3 100 .

Lecture 3. Decimal and Other Notations

The usual notation for numbers is called


decimal or base 10 because it is based on the
number 10.
For example, 1937 stands for the number
1 103 + 9 102 + 3 101 + 7.

The base 3 numeral can


larly by repeatedly dividing b
down the remainders in rever

3.2

Converting base
10

Notice that the digits are obtained, in reverse order, by repeatedly dividing by 10
and discarding the remainders:
1937 divided by 10 is 193, remainder 7.
193 divided by 10 is 19, remainder 3.
19 divided by 10 is 1, remainder 9.
1 divided by 10 is 0, remainder 1.
Thus the remainders, in reverse order, are
1,9,3,7.

We reverse the previous proc


Reading the digits from left
Write down the first digit.
Multiply it by 2 and add the
Multiply the result by 2 and
digit.
..
.

3.1

Example: 1101

Base 2 notation

Exactly the same idea is used to find the


base 2 numeral for a positive integer: repeatedly divide by 2, discard the remainders, and
then write them down in reverse order. E.g.
1937 divided by 2 is 968, remainder 1.
968 divided by 2 is 484, remainder 0.

Add the last digit.

First digit = 1.
Multiplied by 2, plus 2nd dig
Multiplied by 2, plus 3rd dig
Multiplied by 2, plus 4th digi
On an n-digit number, th
volves n 1 multiplications

Example How do we represent 77 in base two?


26 s
64s
1

25 s
32s
0

24 s
16s
0

23 s
8s
1

22 s
4s
1

21 s
2s
0

20 s
1s
1

The base 10 (decimal) number 77 is written as 1001101 in base 2


(binary).

Example Write the decimal number 200 as a sum of powers of two.


200 = 1 128 + 1 64 + 0 32 + 0 16 + 1 8 + 0 4 + 0 2 + 0 1
200 = 1 27 + 1 26 + 0 25 + 0 24 + 1 23 + 0 22 + 0 21 + 0 20

The base 10 (decimal) number 200 is written as 11001000 in base 2


(binary).

1 divided by 10 is 0, remainder 1.
Thus the remainders, in reverse order, are
1,9,3,7.

3.1

Base 2 notation

Exactly the same idea is used to find the


base 2 numeral for a positive integer: repeatedly divide by 2, discard the remainders, and
then write them down in reverse order. E.g.
1937 divided by 2 is 968, remainder 1.
968 divided by 2 is 484, remainder 0.
484 divided by 2 is 242, remainder 0.
242 divided by 2 is 121, remainder 0.
121 divided by 2 is 60, remainder 1.
60 divided by 2 is 30, remainder 0.
30 divided by 2 is 15, remainder 0.
15 divided by 2 is 7, remainder 1.
7 divided by 2 is 3, remainder 1.
3 divided by 2 is 1, remainder 1.
1 divided by 2 is 0, remainder 1.
Thus the base 2 (or binary) numeral for
1937 is 11110010001.

Lecture 3. Decimal and Other Notations

notation for numbers is called


se 10 because it is based on the

The base 3 numeral can be found simi6


larly by repeatedly dividing by 3 and writing
down the remainders in reverse order.

le, 1937 stands for the number


+ 9 102 + 3 101 + 7.

the digits are obtained, in reby repeatedly dividing by 10

3.2

Converting base 2 to base


10

We reverse the previous process.

digit.
..
.
Add the last digit.
Example: 1101

First digit = 1.
Multiplied by 2, plus 2nd digit =
Multiplied by 2, plus 3rd digit =
Multiplied by 2, plus 4th digit =
On an n-digit number, this
volves n 1 multiplications by
additions. It is sometimes call
method, and is more efficient tha
well known method summing

3.3

Summing powers o

The digits in a base 2 numera


cients in a sum of powers of 2.
stands for

1 24 + 0 23 + 0 22 + 1 2

Question 3.2 (b) Convert the binary number 1010111 to decimal.

(1010111)2 = 1 26 + 0 25 + 1 24 + 0 23 + 1 22 + 1 21 + 1 20
(1010111)2 = 1 64 + 0 32 + 1 16 + 0 8 + 1 4 + 1 2 + 1 1
(1010111)2 = 87

otation for numbers is called


e 10 because it is based on the

The base 3 numeral can be found similarly by repeatedly dividing by 3 and writing
down the remainders in reverse order.

, 1937 stands for the number

+ 9 102 + 3 101 + 7.

he digits are obtained, in rey repeatedly dividing by 10


the remainders:
d by 10 is 193, remainder 7.
by 10 is 19, remainder 3.
y 10 is 1, remainder 9.
10 is 0, remainder 1.
mainders, in reverse order, are

2 notation

ame idea is used to find the


for a positive integer: repeat2, discard the remainders, and
m down in reverse order. E.g.
d by 2 is 968, remainder 1.
by 2 is 484, remainder 0.
by 2 is 242, remainder 0.
by 2 is 121, remainder 0.
by 2 is 60, remainder 1.

3.2

Converting base 2 to base


10

We reverse the previous process.


Reading the digits from left to right,
Write down the first digit.
Multiply it by 2 and add the second digit.
Multiply the result by 2 and add the third
digit.
..
.
Add the last digit.
Example: 1101
First digit = 1.
Multiplied by 2, plus 2nd digit = 2 + 1 = 3.
Multiplied by 2, plus 3rd digit = 6 + 0 = 6.
Multiplied by 2, plus 4th digit = 12 + 1 = 13
On an n-digit number, this method involves n 1 multiplications by 2 and n 1
additions. It is sometimes called Horners
method, and is more efficient than the other
well known method summing powers of 2.

by repeatedly dividing by 10
g the remainders:
d by 10 is 193, remainder 7.
by 10 is 19, remainder 3.
by 10 is 1, remainder 9.
y 10 is 0, remainder 1.
mainders, in reverse order, are

Reading the digits from left to right,


Write down the first digit.
Multiply it by 2 and add the second digit.
Multiply the result by 2 and add the third
digit.
..
.
Add the last digit.

2 notation

Example: 1101

ame idea is used to find the


l for a positive integer: repeat2, discard the remainders, and
m down in reverse order. E.g.
d by 2 is 968, remainder 1.
by 2 is 484, remainder 0.
by 2 is 242, remainder 0.
by 2 is 121, remainder 0.
by 2 is 60, remainder 1.
by 2 is 30, remainder 0.
by 2 is 15, remainder 0.
by 2 is 7, remainder 1.
y 2 is 3, remainder 1.
y 2 is 1, remainder 1.
y 2 is 0, remainder 1.
ase 2 (or binary) numeral for
010001.

First digit = 1.
Multiplied by 2, plus 2nd digit = 2 + 1 = 3.
Multiplied by 2, plus 3rd digit = 6 + 0 = 6.
Multiplied by 2, plus 4th digit = 12 + 1 = 13
On an n-digit number, this method involves n 1 multiplications by 2 and n 1
additions. It is sometimes called Horners
method, and is more efficient than the other
well known method summing powers of 2.

3.3

Summing powers of 2

The digits in a base 2 numeral are coefficients in a sum of powers of 2. E.g. 10011
stands for
1 24 + 0 23 + 0 22 + 1 21 + 1 20 .
6

by 2 is 60, remainder 1.
by 2 is 30, remainder 0.
by 2 is 15, remainder 0.
by 2 is 7, remainder 1.
y 2 is 3, remainder 1.
y 2 is 1, remainder 1.
y 2 is 0, remainder 1.
ase 2 (or binary) numeral for
010001.

well known method summing powers of 2.

3.3

Summing powers of 2

The digits in a base 2 numeral are coefficients in a sum of powers of 2. E.g. 10011
stands for
1 24 + 0 23 + 0 22 + 1 21 + 1 20 .
6

Since the coefficients are 0 or 1, this expression is actually a sum of powers of 2, namely
24 + 21 + 20 = 16 + 2 + 1 = 19.
This gives another way to convert binary numerals to decimal.
Example: 11110010001
10

=
=

2 +2 +2 +2 +2 +2
1024 + 512 + 256 + 128 + 16 + 1

1937

This method is convenient if you have the


powers of 2 handy. But it is slower than
Horners method if you have to make the
powers of 2, as it requires more multiplications.

3.4

Base 8 and base 16

3.6

Conversion to hex

The sixteen possible chunks o


digits are
0000 = 0

1000 =

0001 = 1
0010 = 2

1001 =
1010 =

0011 = 3
0100 = 4

1011 =
1100 =

0101 = 5
0110 = 6

1101 =
1110 =

0111 = 7

1111 =

So, e.g.
chunks

11110010001 bre

111 1001 0001





7
9
1

and therefore 11110010001 = (

Question 3.3 (a) Perform 100111 + 1011 in binary.

1 1 1 1

100111
1011
110010

Question 3.3 (c) Perform 10011 101 in binary.

10011
101
10011
000000
1001100
1011111

Question How many different numbers can be stored using eight


binary digits (bits).
There are 28 = 256 different choices for the bits values, so 256.
Note (00000000)2 = 0 and (11111111)2 = 255.

Question 3.4 How would you use eight bits to represent a signed
integer?
A possible method: Use the leftmost bit to indicate whether the
number is positive or negative.
I
I

So (00000011) = 3 and (10000011) = 3, for example.


Can store numbers between 127 = (11111111) and
127 = (01111111).

Note that 0 = (00000000) and 0 = (10000000).

Basic addition routines will need multiple cases.

Twos complement method: Use the leftmost bit as a 128s


column.
I

So 3 = (00000011) and 3 = (11111101), because


3 = 1 128 + 1 64 + 1 32 + 1 16 + 1 8 + 1 4 + 0 2 + 1 1.

We still have that the first bit tells us the sign (1 for negative, 0 for
positive or zero).

Can store numbers between (10000000) = 128 and


(01111111) = 127.

The only way to write 0 is (00000000) = 0.

Basic addition routines just work! (assuming the results are within
the right bounds).

For example 3 + 4 = (11111101) + (00000100) = (00000001) = 1.

Twos complement for n bits.

The leftmost bit represents 2n1 .

The next bit to the right represents 2n2 , the next 2n3 and so on
all the way down to 20 . (These are as per normal binary.)

111 1001 0001





7
9
1

powers of 2, as it requires more multiplications.

3.4

and therefore 11110010001 = (

Base 8 and base 16

Base 2 is the native notation for computers because it requires only two symbols, 0
and 1. However, base 2 numerals are inconveniently long, so they are often rewritten in
a larger base to make them shorter.
The most convenient bases are 8 and 16
(octal and hexadecimal), because they require essentially no conversion from base 2.
All you have to do is break the binary numeral into chunks chunks of three symbols for octal and chunks of four symbols for
hexadecimal.
The three symbol chunks are then translated into

Questions

3.1 Convert the following d


bers to binary.
(a) 37

(b) 81

(c) 255

(d

3.2 Convert the following bin


to decimal and to hexadecima
(a) 1110
(b) 1010111
(d) 11.100001

3.3 Perform the following


binary.
(a) 100111 + 1011 (b) 1010
(c) 10011 101 (d) 11011

0, 1, 2, 3, 4, 5, 6, 7
for octal, and the four symbol chunks are
translated into

3.4 Think of a way of repres


(positive or negative) intege
What are some advantages or d
of your method?

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
for hexadecimal.
7

fficients are 0 or 1, this expresy a sum of powers of 2, namely

+ 20 = 16 + 2 + 1 = 19.

3.6

ther way to convert binary numal.

1110010001
9

+2 +2 +2 +2 +2
+ 512 + 256 + 128 + 16 + 1

d is convenient if you have the


handy. But it is slower than
hod if you have to make the
as it requires more multiplica-

8 and base 16

native notation for computrequires only two symbols, 0


er, base 2 numerals are incon, so they are often rewritten in
to make them shorter.

Conversion to hexadecimal

The sixteen possible chunks of four binary


digits are
0000 = 0

1000 = 8

0001 = 1
0010 = 2

1001 = 9
1010 = A

0011 = 3
0100 = 4

1011 = B
1100 = C

0101 = 5
0110 = 6

1101 = D
1110 = E

0111 = 7

1111 = F

So, e.g.
chunks

11110010001 breaks into the


111 1001 0001



7
9
1

and therefore 11110010001 = (791)16 .

Questions
3.1

Convert the following decimal num-

Question 3.2 (b) (cont.) Convert the binary number 1010111 to


hexadecimal.

(0111)2 = (7)16 ,

(101)2 = (5)16 ,

so (1010111)2 = (57)16 .

Question 3.1 (d)


4s
22 s
1

Convert the decimal 7.625 to binary.


2s
21 s
1

1s
20 s
1

1
2s
1
2 s

1
4s
2
2 s

1
8s
3
2 s

So 7.625 = (111.101)2 .

Question 3.2 (d)


and hexadecimal.
2s
1

Convert the binary number 11.100001 to decimal

1s
1

So (11.100001)2 = 2 + 1 +

1
2s

1
4s

1
8s

1
16 s

1
32 s

1
64 s

1
2

1
64

= 3.515625.

(0011)2 = (3)16 , (1000)2 = (8)16 ,


So (11.100001)2 = (3.84)16 .

(0100)2 = (4)16 .

Potrebbero piacerti anche