Sie sind auf Seite 1von 19

CS231: Computer Architecture I

Mara J. Garzarn
Spring 2008

Number systems

To get started, well 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.
Its 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, well see how to represent numbers with
just 1s and 0s.
0
Then well introduce special operations
0
for computing with 1s and 0s, by treating them as
the logical values true and false.

2000-2002 Howard Huan

Rest of Todays 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

Introduction to CS231

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

Introduction to CS231

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

24
25
26
27

=
=
=
=

16
32
64
128

28 = 256
29 = 512
210 = 1024

Introduction to CS231

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
rem
rem
rem
rem
rem
rem
rem

0
1
0
0
0
1
0
1

0.375 x 2 = 0.750
0.750 x 2 = 1.500
0.500 x 2 = 1.000

So, 162.37510 = 10100010.0112

Introduction to CS231

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 rem 2
16 / 10 = 1
rem 6
1 / 10 = 0
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).
0.375 x 10
= 3.750
The fraction represents
the
remaining digits.
0.750 x 10
= 7.500
0.500 x 10 = 5.000

Introduction to CS231

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.
Its 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.

Decimal
Decimal
00
11
22
33
44
55
66
77
88
99
10
10
11
11
12
12
13
13
14
14
15
15

Introduction to CS231

Binary
Binary
0000
0000
0001
0001
0010
0010
0011
0011
0100
0100
0101
0101
0110
0110
0111
0111
1000
1000
1001
1001
1010
1010
1011
1011
1100
1100
1101
1101
1110
1110
1111
1111

Hex
Hex
00
11
22
33
44
55
66
77
88
99
AA
BB
CC
DD
EE
FF

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 =

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 0100 . 0010 11002
=

C16

Hex

Binary

Hex

Binary

Hex

Binary

Hex

Binary

0000

0100

1000

1100

0001

0101

1001

1101

0010

0110

1010

1110

0011

0111

1011

1111

Introduction to CS231

Number Systems Summary

Computers are binary devices.


Were forced to think in terms of base 2.
Today we learned how to convert numbers between binary, decimal
and hexadecimal.
Weve 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 well introduce special operations for binary values and show how
those correspond to circuits.

Introduction to CS231

10

Boolean Operations

So far, weve talked about how arbitrary numbers can be represented


using just the two binary values 1 and 0.
Now well interpret voltages as the logical values true and false
instead. Well show:
How functions can be defined for expressing computations
How to build circuits that implement our functions in hardware

2000-2002 Howard Huan

11

Boolean values

Volts
Earlier, we used electrical voltages to represent
1.8
two discrete values 1 and 0, from which binary numbers
can be formed.
True
Its 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.

Introduction to CS231

12

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
f(x,y) = 2x + y
=x+x+y
= 2(x + y/2)
= ...

A function table is
unique but infinite
x

f(x,y)

23

41

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.

Introduction to CS231

13

Basic Boolean operations

There are three basic operations for logical values.


Operation:

AND (product)
of two inputs

Expression:

xy, or xy

Truth table:

OR (sum) of
two inputs

NOT
(complement)
on one input

x+y

xy

x+y

Introduction to CS231

14

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)

Introduction to CS231

15

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 orderin 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)

= (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

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

Introduction to CS231

16

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
xy, or xy

OR (sum) of
two inputs
x+y

NOT
(complement)
on one input
x

Logic gate:

Introduction to CS231

17

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

Introduction to CS231

18

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, well look at how Boolean algebra can help simplify
expressions, which in turn will lead to simpler circuits.

Introduction to CS231

19

Das könnte Ihnen auch gefallen