Download Number Systems

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Mathematics of radio engineering wikipedia , lookup

History of the function concept wikipedia , lookup

History of logarithms wikipedia , lookup

List of first-order theories wikipedia , lookup

Functional decomposition wikipedia , lookup

Elementary arithmetic wikipedia , lookup

Principia Mathematica wikipedia , lookup

Laws of Form wikipedia , lookup

Addition wikipedia , lookup

Elementary mathematics wikipedia , lookup

Arithmetic wikipedia , lookup

Location arithmetic wikipedia , lookup

Approximations of π wikipedia , lookup

Positional notation wikipedia , lookup

Transcript
Number systems
•
To get started, we’ll discuss one of the fundamental concepts
underlying digital computer design:
Deep down inside, computers work with just 1s and 0s.
•
•
•
Computers use voltages to represent information. In modern CPUs the
voltage is usually limited to 1.6-1.8V to minimize power consumption.
It’s convenient for us to translate these analog
Volts
voltages into the discrete, or digital, values 1 and 0.
1.8
But how can two lousy digits be useful for anything?
1
– First, we’ll see how to represent numbers with
just 1s and 0s.
– Then we’ll introduce special operations
0
for computing with 1s and 0s, by treating them as
0
the logical values “true” and “false.”
June 10, 2002
©2000-2002 Howard Huang
1
Today’s lecture
•
•
•
Having seen an overview last week,
– We will now start a more thorough study
Number systems
– Review of binary number representation
– How to convert between binary and decimal representations
– Octal and Hex representations
Basic boolean operations
– AND, OR and NOT
– The idea of “Truth Table”
– Boolean functions and expressions
– Truth table for Boolean expressions
June 10, 2002
Number Systems
2
Decimal review
•
Numbers consist of a bunch of digits, each with a weight:
1
100
•
2
1
.
3
1/10
7
1/100
5
Digits
1/1000 Weights
The weights are all powers of the base, which is 10. We can rewrite the
weights like this:
1
102
•
6
10
6
101
2
100
.
3
10-1
7
10-2
5
10-3
Digits
Weights
To find the decimal value of a number, multiply each digit by its weight
and sum the products.
(1 x 102) + (6 x 101) + (2 x 100) + (3 x 10-1) + (7 x 10-2) + (5 x 10-3) = 162.375
June 10, 2002
Number Systems
3
Converting binary to decimal
•
•
We can use the same trick to convert binary, or base 2, numbers to
decimal. The only difference is that the weights are powers of 2.
For example, here is 1101.01 in binary:
1
23
•
1
22
0
21
1
20
.
0
2-1
1
2-2
Binary digits, or bits
Weights (in base 10)
The decimal value is:
(1 x 23) + (1 x 22) + (0 x 21) + (1 x 20) + (0 x 2-1) + (1 x 2-2) =
8
+
4
+
0
+
1
+
0
+ 0.25 = 13.25
Powers of 2:
20 = 1
21 = 2
22 = 4
23 = 8
June 10, 2002
24
25
26
27
=
=
=
=
16
32
64
128
28 = 256
29 = 512
210 = 1024
Number Systems
4
Converting decimal to binary
•
•
•
To convert a decimal integer into binary, keep dividing by 2 until the
quotient is 0. Collect the remainders in reverse order.
To convert a fraction, keep multiplying the fractional part by 2 until it
becomes 0. Collect the integer parts in forward order.
Example: 162.375:
162 / 2
81 / 2
40 / 2
20 / 2
10 / 2
5/2
2/2
1/2
•
= 81
= 40
= 20
= 10
=5
=2
=1
=0
rem 0
rem 1
rem 0
rem 0
rem 0
rem 1
rem 0
rem 1
0.375 x 2 = 0.750
0.750 x 2 = 1.500
0.500 x 2 = 1.000
So, 162.37510 = 10100010.0112
June 10, 2002
Number Systems
5
Why does this work?
•
•
This works for converting from decimal to any base
Why? Think about converting 162.375 from
decimal to decimal.
162 / 10 = 16
16 / 10 = 1
1 / 10 = 0
•
•
rem 2
rem 6
rem 1
Each division strips off the rightmost digit (the
remainder). The quotient represents the remaining
digits in the number.
Similarly, to convert fractions, each multiplication
strips off the leftmost digit (the integer part).
The fraction represents the remaining digits.
0.375 x 10 = 3.750
0.750 x 10 = 7.500
0.500 x 10 = 5.000
June 10, 2002
Number Systems
6
Base 16 is useful too
•
The hexadecimal system uses 16 digits:
0123456789ABCDEF
•
•
•
You can convert between base 10 and base
16 using techniques like the ones we just
showed for converting between decimal and
binary.
For our purposes, base 16 is most useful as
a “shorthand” notation for binary numbers.
– Since 16 = 24, one hexadecimal digit is
equivalent to 4 binary digits.
– It’s often easier to work with a number
like B4 instead of 10110100.
Hex is frequently used to specify things
like 32-bit IP addresses and 24-bit colors.
June 10, 2002
Number Systems
Decimal
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Binary
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
Hex
0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F
7
Binary and hexadecimal conversions
•
Converting from hexadecimal to binary is easy: just replace each hex
digit with its equivalent 4-bit binary sequence.
261.3516 =
2
6
1
.
3
516
= 0010 0110 0001 . 0011 01012
•
To convert from binary to hex, make groups of 4 bits, starting from the
binary point. Add 0s to the ends of the number if needed. Then, just
convert each bit group to its corresponding hex digit.
10110100.0010112 = 1011
=
B
0100 . 0010 11002
4
.
2
C16
Hex
Binary
Hex
Binary
Hex
Binary
Hex
Binary
0
0000
4
0100
8
1000
C
1100
1
0001
5
0101
9
1001
D
1101
2
0010
6
0110
A
1010
E
1110
3
0011
7
0111
B
1011
F
1111
June 10, 2002
Number Systems
8
Number Systems Summary
•
•
•
Computers are binary devices.
– We’re forced to think in terms of base 2.
– Today we learned how to convert numbers between binary, decimal
and hexadecimal.
We’ve already seen some of the recurring themes of architecture:
– We use 0 and 1 as abstractions for analog voltages.
– We showed how to represent numbers using just these two signals.
Next we’ll introduce special operations for binary values and show how
those correspond to circuits.
June 10, 2002
Number Systems
9
Boolean Operations
•
•
So far, we’ve talked about how arbitrary numbers can be represented
using just the two binary values 1 and 0.
Now we’ll interpret voltages as the logical values “true” and “false”
instead. We’ll show:
– How functions can be defined for expressing computations
– How to build circuits that implement our functions in hardware
June 10, 2002
©2000-2002 Howard Huang
10
Boolean values
•
•
•
•
Earlier, we used electrical voltages to represent
Volts
two discrete values 1 and 0, from which binary numbers
1.8
can be formed.
True
It’s also possible to think of voltages as representing
two logical values, true and false.
False
For simplicity, we often still write digits instead:
0
– 1 is true
– 0 is false
We will use this interpretation along with special operations to design
functions and hardware for doing arbitrary computations.
June 10, 2002
Number Systems
11
Functions
•
•
Computers take inputs and produce outputs, just like functions in math!
Mathematical functions can be expressed in two ways:
An expression is
finite but not unique
A function table is
unique but infinite
f(x,y) = 2x + y
=x+x+y
= 2(x + y/2)
= ...
•
x
y
f(x,y)
0
…
2
…
23
…
0
…
2
…
41
…
0
…
6
…
87
…
We can represent logical functions in two analogous ways too:
– A finite, but non-unique Boolean expression.
– A truth table, which will turn out to be unique and finite.
June 10, 2002
Number Systems
12
Basic Boolean operations
•
There are three basic operations for logical values.
Operation:
AND (product)
of two inputs
Expression:
xy, or xy
Truth table:
June 10, 2002
OR (sum) of
two inputs
NOT
(complement)
on one input
x+y
x’
x
y
xy
x
y
x+y
x
x’
0
0
0
0
0
0
0
1
0
1
0
0
1
1
1
0
1
0
0
1
0
1
1
1
1
1
1
1
Number Systems
13
Boolean expressions
•
We can use these basic operations to form more complex expressions:
f(x,y,z) = (x + y’)z + x’
•
•
Some terminology and notation:
– f is the name of the function.
– (x,y,z) are the input variables, each representing 1 or 0. Listing the
inputs is optional, but sometimes helpful.
– A literal is any occurrence of an input variable or its complement.
The function above has four literals: x, y’, z, and x’.
Precedences are important, but not too difficult.
– NOT has the highest precedence, followed by AND, and then OR.
– Fully parenthesized, the function above would be kind of messy:
f(x,y,z) = (((x +(y’))z) + x’)
June 10, 2002
Number Systems
14
Truth tables
•
•
•
A truth table shows all possible inputs and outputs of a function.
Remember that each input variable represents either 1 or 0.
– Because there are only a finite number of values (1 and 0), truth
tables themselves are finite.
– A function with n variables has 2n possible combinations of inputs.
Inputs are listed in binary order—in this example, from 000 to 111.
f(x,y,z) = (x + y’)z + x’
f(0,0,0)
f(0,0,1)
f(0,1,0)
f(0,1,1)
f(1,0,0)
f(1,0,1)
f(1,1,0)
f(1,1,1)
June 10, 2002
= (0 + 1)0 + 1
= (0 + 1)1 + 1
= (0 + 0)0 + 1
= (0 + 0)1 + 1
= (1 + 1)0 + 0
= (1 + 1)1 + 0
= (1 + 0)0 + 0
= (1 + 0)1 + 0
=1
=1
=1
=1
=0
=1
=0
=1
Number Systems
x
y
z
f(x,y,z)
0
0
0
0
1
1
1
1
0
0
1
1
0
0
1
1
0
1
0
1
0
1
0
1
1
1
1
1
0
1
0
1
15
Primitive logic gates
•
Each of our basic operations can be implemented in hardware using a
primitive logic gate.
– Symbols for each of the logic gates are shown below.
– These gates output the product, sum or complement of their inputs.
Operation:
Expression:
AND (product)
of two inputs
OR (sum) of
two inputs
xy, or xy
x+y
NOT
(complement)
on one input
x’
Logic gate:
June 10, 2002
Number Systems
16
Expressions and circuits
•
•
•
Any Boolean expression can be converted into a circuit by combining
basic gates in a relatively straightforward way.
The diagram below shows the inputs and outputs of each gate.
The precedences are explicit in a circuit. Clearly, we have to make sure
that the hardware does operations in the right order!
(x + y’)z + x’
June 10, 2002
Number Systems
17
Boolean operations summary
•
•
•
•
•
•
We can interpret high or low voltage as representing true or false.
A variable whose value can be either 1 or 0 is called a Boolean variable.
AND, OR, and NOT are the basic Boolean operations.
We can express Boolean functions with either an expression or a truth
table.
Every Boolean expression can be converted to a circuit.
Next time, we’ll look at how Boolean algebra can help simplify
expressions, which in turn will lead to simpler circuits.
June 10, 2002
Number Systems
18