The Switch, Part One - Department of Computer and Information

advertisement
The Switch, Part One
CSCI N301: Fundamental Computer
Science Concepts
Copyright ©2006  Department of Computer & Information Science
Goals
By the end of this unit, you should
understand …
• Why computers use switches.
• Why computers encode everything using
binary numbers.
• How to convert to and from Base-2.
• How to convert to and from Base-8.
• How to convert to and from Base-16.
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Why do computers use switches?
• Engineers selected a binary-state
(two state) switch as the fundamental
computer component building block.
• Why? It was simple and reliable and
it supported built-in error checking.
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
The Switch and Architecture
• The innermost physical layer of a
computer is a huge collection of simple,
two state switches.
• We can store any thing we want inside a
computer, as long as we can figure out
how to translate it into a collection of
switches …
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Switches and Meaning
• With one switch, how many messages can we
convey? Two – I could let the open (“ON”)
position represent the message “Yes, we’ll have
class today.”, while the closed (“OFF”) position
stands for “No, we will not.”
• That’s not a lot of information, though, just two
things …
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Putting Them Together
• What if I take two switches, put together?
• Now, I have four things I can say:
–
–
–
–
ON, ON: We’ll have class and a test.
ON, OFF: We’ll have class, but no test.
OFF, ON: We won’t have class, but we’ll have a test.
OFF, OFF: We won’t have class and we won’t have a
test.
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Adding Switches Doubles Message
• 1 switch = 2 message
• 2 switches = 4 messages
• 3 switches = 8 messages
• 4 switches = ? messages
• Do you see a pattern?
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Units of Computer Measurement
• The smallest unit of computer
measurement is a binary digit (or
more commonly, the “bit”).
• The bit represents a single binary
digit, either a 1 or a 0.
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Larger Units of Measurement
•
•
•
•
•
•
•
•
Bit = A single binary digit
Nibble = 4 Bits
Byte = 2 Nibbles = 8 Bits
Word = 2 Bytes = 16 Bits
Double Word = 2 Words = 32 Bits
Kilobyte = 1024 Bytes
Megabyte = 1024 Kilobytes
Gigabyte = 1025 Megabytes
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Starting with Numbers
• Each switch can either have sufficient voltage
(“ON”) or have insufficient voltage (“OFF”).
• Another way of stating voltage: 1 or 0
• This is a common approach in science – to use
abstract, notational systems to describe things.
“1”
“0”
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Numbering Systems
• To figure the answer to this question, let’s step
back and look at numbering systems in general.
• Originally, human being counted with tally math
systems, with a mark for each sheep they were
counting
– If you had 10 sheep, you made 10 marks
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Numbering Systems
• Later, there were advancements in tally math,
using special symbols for special numbers.
– Instead of 10 marks for 10 sheep, let’s make a special
mark for 10 sheep …
=
X
• But this notational system was cumbersome…
you had to learn a lot of symbols, and arithmetic
was difficult
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Positional Numbering Systems
• Wonderful advancement over tally math.
• Let’s keep a few, core numbers (digits).
• We can use these digits to represent
REALLY big numbers if we say that the
value of a number is now made up of 2
parts:
– A count value
– A place value
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Place Values
37
• Consider the number 37…
• The 7 stands for 7 (it’s count value) ones
(it’s positional value). Together, 7 times 1
is 7
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Place Values
37
• Consider the number 37…
• The 3 stands for 3 (it’s count value) tens
(it’s positional value). Together, 3 times 10
is thirty
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Place Values
37
• Consider the number 37…
• Total value: 7 (7 * 1, from the ones
position) and thirty (3 * 10, from the
10 position) is 37!
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Place Values
“Tens”
“Ones”
37
3 * 10
7*1
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Base-10 (Decimal)
• A base refers to the number of digits
(counting numbers) available in a
numbering scheme.
• The highest single digit in base is one less
than the base number.
• In Base-10, the digits are:
– 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Base-10 (Decimal)
• What happens after 9?
• The counter in the right column resets
itself to “0” and 1 is added to the value of
the next left-most column:
09
10
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Base-10 (Decimal)
• Interpreting a multiple digit number simply means
to expand its notation, and write it out by each
positional value:
742
“Hundreds” “Tens” “Ones”
7 * 100
4 * 10
2*1
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Base-10 (Decimal)
• What about using some math to describe the
number:
742
102
101
7 * 102
100
4 * 101 2 * 100
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Using Expanded Notation
• We can use our expanded notation to
figure the value of a number:
2
7*10
= 7*100 = 700
1
4*10 = 4*10 = 40
2*100 =
2*1 =
2
= 74210
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
A Note on Superscripts
• We’ve seen superscripts in
expressions before today. In math,
they are used to indicate an exponent
in an expression:
y
x
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
A Note on Subscripts
• Let’s introduce one more notation –
the subscript. It is used to indicate
the base of a number (the number of
digits available in a numbering
system):
xy
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
A Note on Subscripts
• Remember our previous example?
74210
• It had a subscript of “10”, meaning that
the number we use the Base-10 (Decimal)
numbering system to decode this number.
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Thinking in Binary (Base-2)
• Instead of having 10 digits, the binary (Base-2)
numbering systems has 2 base digits (0 and 1).
– Counting in Base-2: 0, 1, 10, 11, 100, 101, 110, 111,
1000, 1001, 1010 …
• In binary, after the right-most placeholder
reaches one, it resets to 0 and advances the
next left-most placeholder.
• Our base number in binary is 2, therefore we
can use exponents of 2 for placeholder notation
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Binary Placeholders
• Let’s deconstruct 10112:
“Ones”
“Twos”
“Fours”
“Eights”
1011
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Binary Placeholders
• Now, let’s use exponents:
1011
23
22
21
20
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Converting Base-2 to Base-10
• Let’s convert
10112.
• STEP 1: Find all
switches that are
turned “OFF”.
Cross them out
and bring down
the zero.
1011
23
22
21
20
0
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Converting Base-2 to Base-10
• STEP 2: For each
placeholder turned
“ON”, calculate its
exponential
expression.
1011
23
22
21
20
8 0 2 1
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Converting Base-2 to Base-10
• STEP 3: Add the
results of the
exponents. The
sum is the Base-10
equivalent.
1011
23
22
21
20
8 + 0 + 2 + 1=
1110
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Converting from Base-10 to Base-2
• Consider 2310. How can we translate
this to its Base-2 equivalent?
• We’ll use successive division.
Essentially, that means we’ll divide by
2 over and over and keep track of our
remainders.
• Let’s try it …
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Converting Base-10 to Base-2
STEP ONE: Draw a
table with three
columns. Label the
last two columns
quotient and
remainder,
respectively.
Q
R
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Converting Base-10 to Base-2
STEP TWO: In the first
available left-most
column, put the
expression 23/2.
Calculate the division
and put the quotient
and remainder in their
respective columns.
23/2
Q
R
11
1
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Converting Base-10 to Base-2
STEP THREE: Take
the quotient of the
previous
expression and
divide it by two.
Repeat until have 0
for a quotient.
Q
R
23/2
11
1
11/2
5
1
5/2
2
1
2/2
1
0
1/2
0
1
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Converting Base-10 to Base-2
STEP FOUR: Read
the REMAINDER
column from the
bottom to the top.
The 1s and 0s in
the remainder
column represent
the binary number.
Q
R
23/2
11
1
11/2
5
1
5/2
2
1
2/2
1
0
1/2
0
1
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Converting Base-10 to Base-2
2310 = 101112
Q
R
23/2
11
1
11/2
5
1
5/2
2
1
2/2
1
0
1/2
0
1
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Checking the Results
• So.. 2310 = 101112!
• Let’s check our answer by reversing
the process
• 101112 = (1 * 24) + (0 * 23 ) +
( 1 * 22 ) + (1 * 21) + (1 * 20) =
• 16 + 0 + 4 + 2 + 1 = 2310
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Groups of Switches
• We have already seen that a single switch
can’t encode much information. However,
if we group switches together, we can
expand our encoding ability.
• Computers often encode many types of
data, graphics and large numbers for
example, not in Base-2, but in Base-8 or
Base-16.
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Introducing Octal
• Computer scientists are often looking for
shortcuts to do things. One of the ways in
which we can represent Base-2 numbers is
to use their octal (Base-8) equivalents.
• This is especially helpful when we have to
do fairly complicated tasks using large
numbers.
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Introducing Octal
• The octal numbering system includes
eight base digits, 0-7.
• After 7, the next placeholder to the
right begins with a 1:
0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13 ...
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Octal Digits
Base-2
Base-8
Base-10
000
0
0
001
1
1
010
2
2
011
3
3
100
4
4
101
5
5
110
6
6
111
7
7
Click to Return to the Last Slide Viewed
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Octal Placeholders
1
“Ones”
4
“Eights”
2
“Sixty-Fours”
Number:
Value:
64*2
8*4
1*1
Exponential
Expression:
82*2
81*4
80*1
Placeholder
Name:
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Converting Base-2 to Base-8
100011001010012
STEP ONE: Take the binary number and
from right to left, group all placeholders in
triplets. Add leading zeros, if necessary:
010
001
100
101
001
Click to See Octal Digits Table
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Converting Base-2 to Base-8
100011001010012 = 214518
STEP TWO: Convert each triplet to its
single-digit octal equivalent. (HINT: For
each triplet, the octal conversion is the
same as converting to a decimal number):
010
001
100
101
001
2
1
4
5
1
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Converting Base-8 to Base-2
435208 = 1000111010100002
Take each octal digit and convert each
digit to a binary triplet. Insert leading
zeros, if needed:
4
100
3
011
5
101
2
010
0
000
Click to See Octal Digits Table
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Converting Base-8 to Base-10
23748 = N10
Multiply each octal digit by the exponential expression
that represents its placeholder and then add the values:
2*83 = 2*512 = 1024 8
3*82 =
7*81 =
4*80 =
3*64 = 192 8
7*8 =
56 8
4*1 =
4 8
= 127610
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Converting Base-10 to Base-8
To convert Base-10 to Base-8,
we’ll use successive division …
Our new problem:
483210 = ?8
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Converting Base-10 to Base-8
STEP ONE: Draw a
table with three
columns. Label the
last two columns
quotient and
remainder,
respectively.
Q
R
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Converting Base-10 to Base-8
STEP TWO: In the first
available left-most
column, put the
expression 4832/8.
Calculate the division
and put the quotient
and remainder in their
respective columns.
4832/8
Q
R
604
0
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Converting Base-10 to Base-8
STEP THREE: Take
the quotient of the
previous
expression and
divide it by eight.
Repeat until you’ve
have 0 for a
quotient.
Q
R
4832/8
604
0
604/8
75
4
75/8
9
3
9/8
1
1
1/8
0
1
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Converting Base-10 to Base-8
STEP FOUR: Read
the REMAINDER
column from the
bottom to the top.
The numbers in
the remainder
column represent
the octal number.
Q
R
4832/8
604
0
604/8
75
4
75/8
9
3
9/8
1
1
1/8
0
1
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Converting Base-10 to Base-8
483210 = 113408
Q
R
4832/8
604
0
604/8
75
4
75/8
9
3
9/8
1
1
1/8
0
1
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Base-16
• What’s tricky about Base-16?
• This time, we are converting to a
base that has more base digits than
Base-10 (Base-2 and Base-8 have
fewer digits than Base-10)
• So what?
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Hexadecimal Numbering
• There are new symbols for the Base-16
equivalents of the Base-10 numbers 10,
11, 12, 14 and 15. Examine:
DEC
HEX
DEC
HEX
0
0
8
8
1 2 3 4 5 6 7
1 2 3 4 5 6 7
9 10 11 12 13 14 15
9 A B C D E F
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Converting Base-10 to Base-16
To convert Base-10 to Base-16,
we’ll again use successive division …
Our new problem:
21410 = ?16
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Converting Base-10 to Base-16
STEP ONE: Draw a
table with three
columns. Label the
last two columns
quotient and
remainder,
respectively.
Q
R
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Converting Base-10 to Base-16
STEP TWO: In the first
column, write the
expression 214/16.
Calculate the
expression, taking
care not to divide
past the decimal
point. Write the
quotient and
remainder in their
respective columns.
214/16
Q
R
13
6
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Converting Base-10 to Base-16
STEP THREE: Keep
bringing down the
quotient of the
previous
expression and
dividing it by 16
until you have a
quotient of zero.
Q
R
214/16
13
6
13/16
0
13
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Converting Base-10 to Base-16
STEP FOUR: Convert
all remainders to
their hex
equivalents.
Q
R
214/16
13
6=6
13/16
0
13 = D
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Converting Base-10 to Base-16
STEP FIVE: Read the
numbers in the
REMAINDER
column from the
bottom up to get
your hex
equivalent.
Q
R
214/16
13
6=6
13/16
0
13 = D
21410 = D616
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Questions?
N301: Fundamental Computer Science Concepts
Copyright ©2006  Department of Computer & Information Science
Download