2-Number Base Units

Download Report

Transcript 2-Number Base Units

Computer Science
Introduction to the
Number Base Unit
Adapted from Slides by
John Owen
Computer Science Instructor,
Rockport-Fulton High School,
Rockport, Texas
Objective

This unit of study is designed to
introduce the beginner computer
science students to the concept of
the computer number bases (2, 8,
and 16) and their computation.
John Owen, Rockport Fulton HS
2
Part1


Different Number Bases, specifically
about those used by the computer
includes:



Base Two – binary
Base Eight – octal
Base Sixteen – hexadecimal
John Owen, Rockport Fulton HS
3
Base Ten


“because it has ten counting digits,
0,1,2,3,4,5,6,7,8, and 9”
To count in base ten, you go from
0 to 9, then do combinations of two
digits starting with 10 all the way
to 99
John Owen, Rockport Fulton HS
4
Base Two

To count in base two, which only
has 0 and 1 as counting digits, you
count 0,1, then switch to two digit
combinations, 10,11, then to three
digit combos, 100, 101,110,111,
then four digit, 1000,
_____,_______, …, 1111
John Owen, Rockport Fulton HS
5
Base Three

To count in base three, which has
0, 1, and 2 as counting digits, you
count 0,1,2, then switch to two
digit combinations, 10,11, 12, 20,
21, 22, then to three digit combos,
100, 101,102, 110,111, 112, etc…
John Owen, Rockport Fulton HS
6
Base Eight


base eight (often called octal)…
The base eight counting sequence
 0,1,2,3,4,5,6,7,10,11,12,13,…77
 100,101,102,103,104,105,106,10
7110,111, etc.
John Owen, Rockport Fulton HS
7
Base Sixteen

Base Sixteen, also known as
hexadecimal, was especially
created by computer scientists to
help simplify low-level
programming, like machine
language and assembly language.
John Owen, Rockport Fulton HS
8
Base Sixteen
To get sixteen counting digits, you use 09, but still need six more…so it was
decided to use A,B,C,D,E, and F.
 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
 Then the two-digit combos:
10,11,12,…19,1A,1B,1C,1D,1E,1F,20,21
,22,…2D,2E,2F,30,31,…FF

John Owen, Rockport Fulton HS
9
Base conversion

To convert from base ten to another
base, such as base two, eight, or
sixteen, is an important skill for
computer scientists and
programmers.
John Owen, Rockport Fulton HS
10
Base Ten to Base Two


Let’s take the value 27 and convert it
into base 2.
Here’s the process:
Divide 27 by 2
 The answer is 13, remainder 1
 Divide 13 by 2
 Answer is 6, remainder 1

John Owen, Rockport Fulton HS
11
Base Ten to Base Two

Continue until the answer is 1.
6 divided by 2 = 3, remainder 0
 3 divided by 2 = 1, remainder 1



Now take the last answer, 1, and all
of the remainders in reverse order,
and put them together…11011
27 base 10 = 11011 base two
John Owen, Rockport Fulton HS
12
Base Ten to Base Two


Here’s an easy way to do it on
paper
27 divided by 2 = 13, R 1
John Owen, Rockport Fulton HS
13
Base Ten to Base Two

13 / 2 = 6, R 1
John Owen, Rockport Fulton HS
14
Base Ten to Base Two

6 / 2 = 3, R 0
John Owen, Rockport Fulton HS
15
Base Ten to Base Two

3/2
= 1, R 1
John Owen, Rockport Fulton HS
16
Base Ten to Base Two

Stop, and write the answer
John Owen, Rockport Fulton HS
17
Base Ten to Base Two
John Owen, Rockport Fulton HS
18
Exercises

Now try a few yourself (see last
slide for answers):
1.
2.
3.
4.
5.
1610 = _________2
4710 = _________2
14510 = _________2
3110 = _________2
3210 = _________2
John Owen, Rockport Fulton HS
19
Base Ten to Base Eight


Let’s again take the value 27 and
convert it into base 8.
Same process:
Divide 27 by 8
 The answer is 3, remainder 3
 Stop! You can’t divide anymore
because the answer is less than 8

John Owen, Rockport Fulton HS
20
Base Ten to Base Eight

The last answer was 3, and the only
remainder was 3, so the base eight
value is 33, base 8.
John Owen, Rockport Fulton HS
21
Base Ten to Base Eight



Use the same method on paper
27 divided by 8 = 3, R 3
27, base 10 = 33, base 8
John Owen, Rockport Fulton HS
22
Exercises

Now try the same values for base
eight.
1610 = _________8
7. 4710 = _________8
8. 14510 = _________8
9. 3110 = _________8
10. 3210 = _________8
6.
John Owen, Rockport Fulton HS
23
Base Ten to Base Sixteen

Finally we’ll convert 27 into base 16.
Divide 27 by 16
 The answer is 1, remainder 11
 Stop! You can’t divide anymore
because the answer is less than 16

John Owen, Rockport Fulton HS
24
Base Ten to Base Sixteen

The last answer was 1, and the only
remainder was 11, which in base 16
is the letter B, so the base sixteen
value is 1B, base 16.
John Owen, Rockport Fulton HS
25
Base Ten to Base Sixteen



Again, the same method on paper
27 divided by 16 = 1, R 11 or B
27, base 10 = 1B, base 16
John Owen, Rockport Fulton HS
26
Exercises

And now try base sixteen!
1610 = _________16
12. 4710 = _________16
13. 14510 = _________16
14. 3110 = _________16
15. 3210 = _________16
11.
John Owen, Rockport Fulton HS
27
Here are the answers to the
exercises, in jumbled order
10 1F 20 20 2F 37 40
57 91 221 10000 11111
101111 100000 10010001
John Owen, Rockport Fulton HS
28
Part 2: Other conversions

Now you will learn other
conversions among these four
number systems, specifically:
Binary to Decimal
 Octal to Decimal
 Hexadecimal to Decimal

John Owen, Rockport Fulton HS
29
Other conversions

As well as






Binary to Octal
Octal to Binary
Binary to Hexadecimal
Hexadecimal to Binary
Octal to Hexadecimal
Hexadecimal to Octal
John Owen, Rockport Fulton HS
30
Binary to Decimal



Each binary digit in a binary
number has a place value.
In the number 111, base 2, the
digit farthest to the right is in the
“ones” place, like the base ten
system, and is worth 1.
Technically this is the 20 place.
John Owen, Rockport Fulton HS
31
Binary to Decimal



The 2nd digit from the right, 111, is
in the “twos” place, which could be
called the “base” place, and is
worth 2.
Technically this is the 21 place.
In base ten, this would be the
“tens” place and would be worth
10.
John Owen, Rockport Fulton HS
32
Binary to Decimal



The 3rd digit from the right, 111, is
in the “fours” place, or the “base
squared” place, and is worth 4.
Technically this is the 22 place.
In base ten, this would be the
“hundreds” place and would be
worth 100.
John Owen, Rockport Fulton HS
33
Binary to Decimal


The total value of this binary
number, 111, is 4+2+1, or seven.
In base ten, 111 would be worth
100 + 10 + 1, or one-hundred
eleven.
John Owen, Rockport Fulton HS
34
Binary to Decimal

Can you figure the decimal values
for these binary values?
11
 101
 110
 1111
 11011

John Owen, Rockport Fulton HS
35
Binary to Decimal

answers:
11 is 3 in base ten
 101 is 5
 110 is 6
 1111 is 15
 11011 is 27

John Owen, Rockport Fulton HS
36
Octal to Decimal



Octal digits have place values
based on the value 8.
In the number 111, base 8, the
digit farthest to the right is in the
“ones” place and is worth 1.
Technically this is the 80 place.
John Owen, Rockport Fulton HS
37
Octal to Decimal


The 2nd digit from the right, 111, is
in the “eights” place, the “base”
place, and is worth 8.
Technically this is the 81 place.
John Owen, Rockport Fulton HS
38
Octal to Decimal


The 3rd digit from the right, 111, is
in the “sixty-fours” place, the “base
squared” place, and is worth 64.
Technically this is the 82 place.
John Owen, Rockport Fulton HS
39
Octal to Decimal

The total value of this octal
number, 111, is 64+8+1, or
seventy-three.
John Owen, Rockport Fulton HS
40
Octal to Decimal

Can you figure the value for these
octal values?
21
 156
 270
 1164
 2105

John Owen, Rockport Fulton HS
41
Octal to Decimal

Here are the answers:
21 is 17 in base 10
 156 is 110
 270 is 184
 1164 is 628
 2105 is 1093

John Owen, Rockport Fulton HS
42
Hexadecimal to Decimal



Hexadecimal digits have place
values base on the value 16.
In the number 111, base 16, the
digit farthest to the right is in the
“ones” place and is worth 1.
Technically this is the 160 place.
John Owen, Rockport Fulton HS
43
Hexadecimal to Decimal


The 2nd digit from the right, 111, is
in the “sixteens” place, the “base”
place, and is worth 16.
Technically this is the 161 place.
John Owen, Rockport Fulton HS
44
Hexadecimal to Decimal


The 3rd digit from the right, 111, is
in the “two hundred fifty-six” place,
the “base squared” place, and is
worth 256.
Technically this is the 162 place.
John Owen, Rockport Fulton HS
45
Hexadecimal to Decimal

The total value of this hexadecimal
number, 111, is 256+16+1, or two
hundred seventy-three.
John Owen, Rockport Fulton HS
46
Hexadecimal to Decimal

Can you figure the value for these
hexadecimal values?
2A
 15F
 A7C
 11BE
 A10D

John Owen, Rockport Fulton HS
47
Hexadecimal to Decimal

Here are the answers:
2A is 42 in base 10
 15F is 351
 A7C is 2684
 11BE is 4542
 A10D is 41229

John Owen, Rockport Fulton HS
48
Binary to Octal


The conversion between binary and
octal is quite simple.
Since 2 to the power of 3 equals 8,
it takes 3 base 2 digits to combine
to make a base 8 digit.
John Owen, Rockport Fulton HS
49
Binary to Octal








000 base 2 equals 0 base 8
0012 = 18
0102 = 28
0112 = 38
1002 = 48
1012 = 58
1102 = 68
1112 = 78
John Owen, Rockport Fulton HS
50
Binary to Octal



What if you have more than three
binary digits, like 110011?
Just separate the digits into groups
of three from the right, then
convert each group into the
corresponding base 8 digit.
110 011 base 2 = 63 base 8
John Owen, Rockport Fulton HS
51
Binary to Octal

Try these:
111100
 100101
 111001
 1100101




Hint: when the leftmost group has fewer than three
digits, fill with zeroes from the left:
1100101 = 1 100 101 = 001 100 101
110011101
John Owen, Rockport Fulton HS
52
Binary to Octal

The answers are:
1111002 = 748
 1001012 = 458
 1110012 = 718
 11001012 = 1458
 1100111012 = 6358

John Owen, Rockport Fulton HS
53
Binary to Hexadecimal


The conversion between binary and
hexadecimal is equally simple.
Since 2 to the power of 4 equals
16, it takes 4 base 2 digits to
combine to make a base 16 digit.
John Owen, Rockport Fulton HS
54
Binary to Hexadecimal








0000 base 2 equals 0 base 8
00012 = 116
00102 = 216
00112 = 316
01002 = 416
01012 = 516
01102 = 616
01112 = 716
John Owen, Rockport Fulton HS
55
Binary to Hexadecimal








10002
10012
10102
10112
11002
11012
11102
11112
=
=
=
=
=
=
=
=
816
916
A16
B16
C16
D16
E16
F16
John Owen, Rockport Fulton HS
56
Binary to Hexadecimal


If you have more than four binary
digits, like 11010111, again
separate the digits into groups of
four from the right, then convert
each group into the corresponding
base 16 digit.
1101 0111 base 2 = D7 base 16
John Owen, Rockport Fulton HS
57
Binary to Hexadecimal

Try these:
11011100
 10110101
 10011001
 110110101




Hint: when the leftmost group has fewer than four
digits, fill with zeroes on the left:
110110101 = 1 1011 0101 = 0001 1011 0101
1101001011101
John Owen, Rockport Fulton HS
58
Binary to Hexadecimal

The answers are:
110111002 = DC16
 101101012 = B516
 100110012 = 9916
 1101101012 = 1B516
 1 1010 0101 11012 = 1A5D16

John Owen, Rockport Fulton HS
59
Octal to Binary



Converting from Octal to Binary is
just the inverse of Binary to Octal.
For each octal digit, translate it into
the equivalent three-digit binary
group.
For example, 45 base 8 equals
100101 base 2
John Owen, Rockport Fulton HS
60
Hexadecimal to Binary



Converting from Hexadecimal to
Binary is the inverse of Binary to
Hexadecimal.
For each “hex” digit, translate it
into the equivalent four-digit binary
group.
For example, 45 base 16 equals
01000101 base 2
John Owen, Rockport Fulton HS
61
Octal and Hexadecimal to
Binary Exercises

Convert each of these to binary:
638
 12316
 758
 A2D16
 218
 3FF16

John Owen, Rockport Fulton HS
62
Octal and Hexadecimal to
Binary Exercises

The answers are:
638 = 1100112
 12316 = 1001000112 (drop leading 0s)
 758 = 1111012
 A2D16 = 1100001011012
 218 = 100012
 3FF16 = 11111111112

John Owen, Rockport Fulton HS
63
Hexadecimal to Octal



Converting from Hexadecimal to
Octal is a two-part process.
First convert from “hex” to binary,
then regroup the bits from groups
of four into groups of three.
Then convert to an octal number.
John Owen, Rockport Fulton HS
64
Hexadecimal to Octal

For example:
4A316
 = 0100 1010 00112
 = 010 010 100 0112
 = 22438

John Owen, Rockport Fulton HS
65
Octal to Hexadecimal



Converting from Octal to
Hexadecimal is a similar two-part
process.
First convert from octal to binary,
then regroup the bits from groups
of three into groups of four.
Then convert to an hex number.
John Owen, Rockport Fulton HS
66
Hexadecimal to Octal

For example:
3718
 = 011 111 0012
 = 1111 10012
 = F98

John Owen, Rockport Fulton HS
67
Octal/Hexadecimal Practice

Convert each of these:
638 = ________16
 12316 = ________8
 758 = ________16
 A2D16 = ________8
 218 = ________16
 3FF16 = ________8

John Owen, Rockport Fulton HS
68
Octal/Hexadecimal Practice

The answers are
638 = 3316
 12316 = 4438
 758 = 3D16
 A2D16 = 50558
 218 = 1116
 3FF16 = 17778

John Owen, Rockport Fulton HS
69
Part3: Counting, Place Value

An introduction to the basic idea
of counting in different bases and
the place value system,
associating it with the familiar
base 10 system.
John Owen, Rockport Fulton HS
70
Review Base Ten Addition, #1



In Base 10 addition, you learned a
very simple process.
Look at this problem:
12
+37
First add the ones column, then the
tens.
John Owen, Rockport Fulton HS
71
Review Base Ten Addition, #1
12
+37
49

The answer is 49…simple, right?
John Owen, Rockport Fulton HS
72
Review Base Ten Addition, #2


Now look at this problem:
13
+37
When you add the ones column
values, the result of 10 EQUALS the
base value of 10, so you have to
CARRY a 1.
John Owen, Rockport Fulton HS
73
Review Base Ten Addition, #2

1
13
+37
0
When a carry is made, you essentially
divide by 10 (the base) to determine
what value to carry, and mod by 10 to
determine what value to leave behind.
John Owen, Rockport Fulton HS
74
Review Base Ten Addition,#2



1
13
+37
0
3 plus 7 is 10
10 divided by 10 is 1 (carry)
10 mod 10 is 0 (leave)
John Owen, Rockport Fulton HS
75
Review Base Ten Addition, #2
1
13
+37
50

Answer is 50
John Owen, Rockport Fulton HS
76
Review Base Ten Addition, #3


Here’s a third example:
16
+37
When you add the ones column
values, the result of 13 EXCEEDS
the base value of 10, so CARRY a
1.
John Owen, Rockport Fulton HS
77
Review Base Ten Addition, #3
16
+37



6 plus 7 is 13
13 divided by 10 is 1 (carry)
13 mod 10 is 3 (leave)
John Owen, Rockport Fulton HS
78
Review Base Ten Addition, #3

1
16
+37
53
Answer is 53
John Owen, Rockport Fulton HS
79
Review Base Ten Addition, #4


And finally, a fourth example:
76
+35
The ones column result of 11
EXCEEDS the base value of 10, and
you CARRY a 1.
John Owen, Rockport Fulton HS
80
Review Base Ten Addition,#4



1
76
+35
1
6 plus 5 is 11
11 divided by 10 is 1 (carry)
11 mod 10 is 1 (leave)
John Owen, Rockport Fulton HS
81
Review Base Ten Addition, #4



1
76
+35
1
1+7+3 is 6 plus 5, which equals 11
11 divided by 10 is 1 (carry)
11 mod 10 is 1 (leave)
John Owen, Rockport Fulton HS
82
Review Base Ten Addition, #4
1

76
+35
111
Answer is 111, base 10
John Owen, Rockport Fulton HS
83
Base Eight Addition, #1


Now here is an example in base
eight:
12
+34
When you add the ones column
values, the answer is 6, and the
second column answer is 4.
John Owen, Rockport Fulton HS
84
Base Eight Addition. #1



12
+34
48
Answer is 48, base eight
You say, “four eight base eight”, not
“forty-eight”
The phrase “forty-eight” is meant for
base ten only.
John Owen, Rockport Fulton HS
85
Base Eight Addition, #2


Now look at this problem:
14
+34
When you add the ones column
values, the result of 8 EQUALS the
base value of 8, and you have to
CARRY a one.
John Owen, Rockport Fulton HS
86
Base Eight Addition, #2
14
+34

Again you divide by 8 (the base) to
determine what value to carry, and
mod by 8 to determine what value
to leave behind.
John Owen, Rockport Fulton HS
87
Base Eight Addition, #2



1
14
+34
0
4 plus 4 is 8
8 divided by 8 is 1 (carry)
8 mod 8 is 0 (leave)
John Owen, Rockport Fulton HS
88
Base Eight Addition, #2


1
14
+34
50
Answer is “five zero, base eight”!
Looks strange, but it is correct!
John Owen, Rockport Fulton HS
89
Base Eight Addition, #3


Here’s a third example:
16
+37
When you add the ones column
values, the result of 13 EXCEEDS
the base value of 8, and you have
to CARRY a one.
John Owen, Rockport Fulton HS
90
Base Eight Addition, #3



1
16
+37
5
6 plus 7 is 13
13 divided by 8 is 1 (carry)
13 mod 8 is 5 (leave)
John Owen, Rockport Fulton HS
91
Base Eight Addition, #3

1
16
+37
55
Answer is 55, base eight.
John Owen, Rockport Fulton HS
92
Base Eight Addition, #4


And a fourth example:
76
+35
The ones column result of 11
EXCEEDS the base value of 8,
…CARRY a one.
John Owen, Rockport Fulton HS
93
Base Eight Addition, #4



1
76
+35
3
6 plus 5 is 11
11 divided by 8 is 1 (carry)
11 mod 8 is 3 (leave)
John Owen, Rockport Fulton HS
94
Base Eight Addition, #4



1
76
+35
33
1+7+3 is 6 plus 5 is 11
11 divided by 8 is 1 (carry)
11 mod 8 is 3 (leave)
John Owen, Rockport Fulton HS
95
Base Eight Addition, #4
1
76
+35
133
Answer is 133, base 8
John Owen, Rockport Fulton HS
96
Base Two Addition, #1


Base Two Addition is quite
interesting, but also fairly simple.
Since the only counting digits in
base two are the values 0 and 1,
there are only a few situations you
have to learn.
John Owen, Rockport Fulton HS
97
Base Two Addition, #1


We’ll start simple:
1
+1
=10 (“one zero, base two”)
This looks strange, but the same
process applies.
John Owen, Rockport Fulton HS
98
Base Two Addition, #1

1
+1
= 10
Since 1 + 1 is 2, this EQUALS the
base value of 2, which means you
carry the “div” answer and leave
the “mod” answer
John Owen, Rockport Fulton HS
99
Base Two Addition, #1



1
+1
= 10
2 / 2 = 1 (carry)
2 % 2 = 0 (leave)
That’s it!
John Owen, Rockport Fulton HS
100
Base Two Addition, #2


Here’s another:
10
+11
= 101
Can you figure it out?
John Owen, Rockport Fulton HS
101
Base Two Addition, #2


10
+11
= 101
In the ones column, 1 + 0 is 1.
In the second column, 1+1 is 2, or
10 in base 2
John Owen, Rockport Fulton HS
102
Base Two Addition, #3


And another:
101101
+110011
=
Can you figure it out?
John Owen, Rockport Fulton HS
103
Base Two Addition, #3

Step by step…
1
101101
+110011
=
0
John Owen, Rockport Fulton HS
104
Base Two Addition, #3

Step by step…
1
101101
+110011
=
00
John Owen, Rockport Fulton HS
105
Base Two Addition, #3

Step by step…
1
101101
+110011
=
000
John Owen, Rockport Fulton HS
106
Base Two Addition, #3

Step by step…
1
101101
+110011
= 0000
John Owen, Rockport Fulton HS
107
Base Two Addition, #3


Step by step…
1
101101
+110011
= 00000
Since 1+1+1 is 3, carry 1 and
leave 1
John Owen, Rockport Fulton HS
108
Base Two Addition, #3


Step by step…
1
101101
+110011
=1100000
All done!
John Owen, Rockport Fulton HS
109
Base Sixteen, Example #1


In base sixteen, remember the
digits are 0-9, then A-F,
representing the values 0-15
Here’s an example:
29
+12
John Owen, Rockport Fulton HS
110
Base Sixteen, Example #1


29
+12
= 3B, base 16
2 + 9 is 11, which is B in base
sixteen
2+1 is 3, so the answer is 3B
John Owen, Rockport Fulton HS
111
Base Sixteen, Example #2


1
A9
+47
= F0, base 16
9+7 is 16, equal to the base, so
carry 1 and leave 0
1 + A(10) + 4 is 15, which is F
John Owen, Rockport Fulton HS
112
Base Sixteen, Example #3


11
D6
+7C
= 152, base 16
6+C(12) = 18, carry 1, leave 2
1+D(13)+7 = 21, carry 1, leave 5
John Owen, Rockport Fulton HS
113
Base Sixteen, Example #4


11
EF
+2D
= 11C, base 16
F(15) + D(13) = 28, carry 1, leave
C(12)
1 + E(14) + 2 = 17, carry 1, leave 1
John Owen, Rockport Fulton HS
114
Exercises

1.
2.
3.
4.
5.
6.
Now try these exercises
12 + 12 =
78 + 68 =
F16 + F16 =
58 + 58 =
916 + B16 =
C16 + D16 =
John Owen, Rockport Fulton HS
115
Exercises
7.
8.
9.
10.
11.
12.
13.
38 + 4 8 =
F16 + 216 =
102 + 102 =
12 + 10112 =
102 + 1102 =
2168 + 3648 =
7778 + 38 =
John Owen, Rockport Fulton HS
116
Exercises
14.
15.
16.
ACE16 + BAD16 =
23416 + 97516 =
4216 + F16 + 87616 =
John Owen, Rockport Fulton HS
117
ANSWERS (JUMBLED)
7
11 12 10 14 15 19
1E 100
1002
602
1100
BA9 8C7
1000
167B
John Owen, Rockport Fulton HS
118
Part 4


In this section you’ll learn how to
do subtraction and how to solve
simple equations involving Base 2,
8, and 16.
Again, it is essentially the same
concept as Base 10, just in a
different base!
John Owen, Rockport Fulton HS
119
Review Base Ten Subtraction


In Base 10 subtraction, you use a
very simple process.
Look at this problem:
48
-37
= 11
John Owen, Rockport Fulton HS
120
Review Base Ten Subtraction

48
-37
= 11
Each column is subtracted to get
an answer of 11…
John Owen, Rockport Fulton HS
121
Subtraction, Base 10


Now look at this problem:
63
-37
In this problem, you need to
borrow.
John Owen, Rockport Fulton HS
122
Subtraction, Base 10
513
63
-37

Borrowing means taking a value
from the next column and adding it
to the column you need.
John Owen, Rockport Fulton HS
123
Subtraction, Base 10
513
63
-37

In this case, borrow from the 6,
which becomes five, and add 10 to
the 3, making 13.
John Owen, Rockport Fulton HS
124
Subtraction, Base 10
513
63
-37

When you borrow 1 from one column, it
becomes the value of the base in the
next column, or 10 in this case.
John Owen, Rockport Fulton HS
125
Subtraction, Base 10
513
63
-37
26

Then you subtract the two columns
with a result of 26.
John Owen, Rockport Fulton HS
126
Subtraction, Base 8


Now let’s try base eight:
63
-37
Again, in this problem, you need to
borrow.
John Owen, Rockport Fulton HS
127
Subtraction, Base 8
511
63
-37

Borrow from the 6, which becomes
five, and add 8 to the 3, making
11!
John Owen, Rockport Fulton HS
128
Subtraction, Base 8
511
63
-37

When you borrow 1 from a column,
it becomes the value of the base in
the next column, or 8 in this case.
John Owen, Rockport Fulton HS
129
Subtraction, Base 8
511
63
-37
24

Then you subtract the two columns
with a result of 24, base 8.
John Owen, Rockport Fulton HS
130
Subtraction, Base 16
Now base 16:
519
63
-37

Again, we borrow from the 6, which
becomes five, and add 16 to the 3,
making 19!
John Owen, Rockport Fulton HS
131
Subtraction, Base 16
519
63
-37

When you borrow 1 from a column, it
becomes the value of the base in the
next column, or 16 in this case.
John Owen, Rockport Fulton HS
132
Subtraction, Base 16

519
63
-37
2C
In the ones column, 19 minus 7 is
12, which is C in base sixteen, with
2 in the second column.
John Owen, Rockport Fulton HS
133
Subtraction, Base 16
Here’s another example in base 16
D6
-3B

How is this one solved? Try it.
John Owen, Rockport Fulton HS
134
Subtraction, Base 16
C22
D6
-3B

We must borrow from D, which
becomes C, then add 16 to 6,
which makes 22.
John Owen, Rockport Fulton HS
135
Subtraction, Base 16
C22
D6
-3B
9B



22 minus B (11) is B.
C minus 3 is 9.
Answer is 9B
John Owen, Rockport Fulton HS
136
Subtraction, Base 2
Now base 2:
11
- 1
10

This one is easy…answer is 10
John Owen, Rockport Fulton HS
137
Subtraction, Base 2
Another in base 2:
02
110
- 1

Here we need to borrow from the
twos place…
John Owen, Rockport Fulton HS
138
Subtraction, Base 2
02
110
- 1
101
Subtract to get the answer.
John Owen, Rockport Fulton HS
139
Subtraction, Base 2
Still another in base 2:
02
110
- 11
1
Now borrow again…
John Owen, Rockport Fulton HS
140
Subtraction, Base 2
2
100
- 11
01
Final answer is 01, base 2
John Owen, Rockport Fulton HS
141
Simple Equations
Here an equation to solve (base 10):
x + 6 = 14
John Owen, Rockport Fulton HS
142
Simple Equations
Solution…subtract 6 from both sides
x + 6 = 14
-6
-6
x
= 8
John Owen, Rockport Fulton HS
143
Simple Equations
Now do it in base 8:
x + 6 = 14
John Owen, Rockport Fulton HS
144
Simple Equations
Solution…subtract 6 from both sides
x + 6 = 14
-6
-6
x
= ?
John Owen, Rockport Fulton HS
145
Simple Equations
Answer is 6, base 8
12
x + 6 = 14
-6
-6
x
= 6
John Owen, Rockport Fulton HS
146
Simple Equations
Here’s an equation in base sixteen
(remember, A and F are NOT variables,
but base sixteen values):
x + 2A = F3
John Owen, Rockport Fulton HS
147
Simple Equations
Solution?
x + 2A = F3
John Owen, Rockport Fulton HS
148
Simple Equations
Subtract 2A from both sides:
E19
x + 2A = F3
- 2A -2A
x
= C9
John Owen, Rockport Fulton HS
149
Exercises

1.
2.
3.
4.
5.
6.
Now try these exercises
12 - 12 =
78 - 68 =
F16 - A16 =
158 - 68 =
4916 - 2B16 =
CC16 - AD16 =
John Owen, Rockport Fulton HS
150
Exercises
7.
8.
9.
10.
11.
12.
13.
738 - 348 =
3E16 – 2F16 =
1012 - 102 =
11012 - 112 =
10102 - 1112 =
7168 - 3648 =
7768 + 3378 =
John Owen, Rockport Fulton HS
151
Exercises
Now let’s mix it up a bit!
14. AE16 + 768 = _________8
15. 2348 + 110110112 = _________16
16. 101102 - F16 + 768 = _________10
17. 38 + 3910 - 1101012 = _________16
18. 11112 - F16 + 1510 = _________16
John Owen, Rockport Fulton HS
152
Exercises
And finally, some equations
19. x16 + 7616 = AB16
20. x2 - 10112 = 1012
21. x8 + 568 = 728
22. x2 + 2510 = 1F16
23. x8 + 3748 - 65568 = BAD16
24. 378 + X16 = 110111102
John Owen, Rockport Fulton HS
153
ANSWERS (JUMBLED)
0
1
5
7
11
11
14
19
35
37
69
110
177
332
354
1010
1335
10000
14037
1E
1F
BF
F
F
John Owen, Rockport Fulton HS
154
Representing Fractional Numbers

Computers store fractional numbers


Storage technique based on floating-point
notation



Example: 1.345E+5
1.345 = mantissa, E = exponent, + 5 moves decimal
IEEE-754 specification


Negative and positive
Uses binary mantissas and exponents
Implementation details are part of advanced
study
155
Conversion to Decimal

(dn…..d2 d1 d0 . d-1 d-2 ……)
R
=(
)10
Examples
Example
Part5:
Data Representation in Binary

Binary values map to two states


Bit


Group of 8 bits
Word


Each 1 and 0 (on and off ) in a computer
Byte


On or off
Collection of bytes (typically 4 bytes)
Nibble

Half a byte or 4 bits
Connecting with Computer Science, 2e
159
Representing Whole Numbers

Whole numbers (integer numbers)


Stored in fixed number of bits
2010 stored as 16-bit integer 0000011111011010


Equivalent hex value: 07DA
Signed numbers stored with twos complement

Leftmost bit reserved for sign



1 = negative and 0 = positive
If positive: leave as is
If negative: perform twos complement

Reverse bit pattern and add 1 to number using binary
addition
160
Representing Whole Numbers (cont’d.)
Figure 7-5, Storing numbers in a twos complement 8-bit field
161
One’s Complement


Way to represent negative values.
Change every one in binary to zero and
every zero to 1
Example

Convert (1010011)2 one’s complement.


(0101100)
1’s
Convert (1000010011) which is one’s
complement to its binary value.

(0111101100)2
Two’s complement



It is a way to represent a number with
both value and sign.
The most important property is that the
Most Significant Bit has a negative
weight.
To convert from two’s complement to
decimal is an easy way. The only
difference is that during computation
the weight of the last bit is negative.
Example to convert from two’s
complement to decimal.
Converting from decimal
number to two’s complement

We have two cases.


If it is a positive decimal number. It is the
same as normal binary except that you add 0
as MSB to the binary number.
If it is negative decimal number.





Treat the number as if it is positive.
Convert it to binary number.
Put Zeroes to the left of the number.
Convert the zero to one and one to zero for all
digits
Add one to the whole number.
Subtraction using two’s
complement

Subtraction process requires two operands:
minuend and subtrahend, normally the
subtrahend is greater than the minuend.




Make sure you have more digits that
accommodate the value of the numbers by adding
zeros to the left of the two numbers.
Convert the subtrahend to two’s complement.
Add the two numbers.
If the number of digits of the result exceeds
numbers of digits both numbers. Cancel that digit.
Example of Two’s complement
subtraction.

Subtract 1001 from 101.

Number of digits ( we need 5 digits for those numbers) so ,
we add trailing zeros


Convert the subtrahend to two’s complement


So it 01001 becomes 10111
Add both numbers: 00101 to 10111


Minuend becomes 00101 and the subtrahend becomes 01001
00101 +10111 = 11100
As the number of digits of the result is that same as the
number of the digits of both number. This is the result.

(Note that as the last bit of the result is 1, it means the number is negative
, to find its value. Follow the rules to convert from two’s complement to
decimal)
Example of Two’s complement
subtraction.

Subtract 110 from 1011.

Number of digits ( we need 5 digits for those numbers) so ,
we add trailing zeros


Convert the subtrahend to two’s complement


So it 00110 becomes 11010
Add both numbers: 01011 to 11010


Minuend becomes 01011 and the subtrahend becomes 00110
01011+ 11010 = 100101
Because we have six digits, the left most digits is discarded
and keep the number of the digits to 5. the result will b
00101

(Note that as the last bit of the result is 0, it means the number is positive,
to find its value. Follow the rules to convert from two’s complement to
decimal)
Exercises 1-5

1.
2.
3.
4.
5.
Convert each of these to base 2
528
2738
6178
44728
35028
John Owen, Rockport Fulton HS
170
Exercises 6 - 10

6.
7.
8.
9.
10.
Convert each of these to base 2
67316
2A516
DEB16
50C16
293716
John Owen, Rockport Fulton HS
171
Exercises 11 - 15

11.
12.
13.
14.
15.
Convert each of these to base 2
4510
3610
1710
7210
5710
John Owen, Rockport Fulton HS
172
Exercises 16 - 20

16.
17.
18.
19.
20.
Convert each of these to base 8
1100112
11110012
100101102
110110112
1101001012
John Owen, Rockport Fulton HS
173
Exercises 21 - 25

21.
22.
23.
24.
25.
Convert each of these to base 8
45316
D1016
72916
BCEF16
4A616
John Owen, Rockport Fulton HS
174
Exercises 26 - 30

26.
27.
28.
29.
30.
Convert each of these to base 8
4610
8910
7010
12010
27310
John Owen, Rockport Fulton HS
175
Exercises 31 - 35

31.
32.
33.
34.
35.
Convert each of these to base 16
1011012
11100012
110011012
10001001012
11000010012
John Owen, Rockport Fulton HS
176
Exercises 36 - 40

36.
37.
38.
39.
40.
Convert each of these to base 16
468
2768
7258
56248
70138
John Owen, Rockport Fulton HS
177
Exercises 41 - 45

41.
42.
43.
44.
45.
Convert each of these to base 16
7410
15610
16810
41510
55010
John Owen, Rockport Fulton HS
178
ANSWERS…JUMBLED!
26
56
63
71
106
131
170
171
225
226
226
309
333
421
645
2123
2246
3451
6420
10001
100100
101010
101101
111001
136357
110001111
1010100101
10100001100
11001110011
11101000010
100100111010
110111101011
10010000
10100100110111
10111011
John Owen, Rockport Fulton HS
19F
1D5
2D
4A
9C
A8
B94
BE
CD
EOB
179