Turing Mac hines Universal T uring Mac hineagents.csie.ntu.edu.tw/~yjhsu/courses/IntroCS/2008... ·...

Post on 20-May-2020

3 views 0 download

Transcript of Turing Mac hines Universal T uring Mac hineagents.csie.ntu.edu.tw/~yjhsu/courses/IntroCS/2008... ·...

The World of 0 and 1Introduction to Computers

Fall 2008

Adapted from slides by Prof. Polly Huang (National Taiwan University)

Von Neumann Architecture• a processing unit

• a single separate storage structure to hold both instructions and data

• implements a universal Turing machine

3

• Simple abstract computation devices to investigate what can be computed

• Reduce computation to the simplest and the most abstract form

• State machine

• States of mind

• Transitions from one state to another

• Read/write head and a paper tape

Turing Machines Universal Turing Machine

5

• Suppose that you want to buy the hottest new cell phone in the market

• It costs about NT$7000

• What is the thinking process involved in collecting the NT$7000?

• Ask parents

• If not enough, check bank saving

• If not enough, borrow from friends

• If not enough, get a part-time job

Real-Life Example

6

StartMe

poor

Done

Parents not

generousFriends

bad

cash=cash+addcash<7000

Work

more

cash>=7000

• Ask parents

• If not enough, check bank saving

• If not enough, borrow from friends

• If not enough, get a part-time job

• …

cash=cash+addcash<7000

cash=cash+addcash<7000

cash=cash+addcash<7000

The Tape

2000

5000

7000

The Machine

• What does this Turing machine do?

state read write move next state

a blank 0 R b

b blank R c

c blank 1 R d

d blank R a

0 1

A Simple Example

© Jane HsuTuring Machine

Example: Add 1

• Let n be represented by n 1’s

8

+1

_ 1 1 1 1 _ _ _ _ _ …

_ 1 1 1 1 1 _ _ _ _

( 1 , 1 , )

go done

( _ , 1 , )

© Jane HsuTuring Machine 9

Example: Unary Adder

Let n be represented by (n+1) 1’s

2 1

+

3

2+1=3

+

_ 1 1 1 0 1 1 _ _ _ …

_ 1 1 1 1 _ _ _ _ _

© Jane HsuTuring Machine 10

One Solution

• Turn the 0 to 1

• Erase the two 1’s at the end

+

_ 1 1 1 1 _ _ _ _ _

_ 1 1 1 0 1 1 _ _ _ …

© Jane HsuTuring Machine 11

State Diagram for the Unary Adder

( 1 , 1 , )

go d1 d2 back

( _ , _ , )

( 0 , 1 , )

( 1 , _ , )( 1 , _ , )

( 1 , 1 , )

( _ , _ , )

done

+

_ 1 1 1 0 1 1 _ _ _ …

_ 1 1 1 1 _ _ _ _ _ …

© Jane HsuTuring Machine 12

_ 1 1 1 0 1 1 _ _ _ … start

go_ 1 1 1 0 1 1 _ _ _ …

go_ 1 1 1 0 1 1 _ _ _ …

go_ 1 1 1 0 1 1 _ _ _ …

go_ 1 1 1 0 1 1 _ _ _ …

go_ 1 1 1 1 1 1 _ _ _ …

go_ 1 1 1 1 1 1 _ _ _ …

go_ 1 1 1 1 1 1 _ _ _ …

d1_ 1 1 1 1 1 1 _ _ _ …

d2_ 1 1 1 1 1 _ _ _ _ …

back_ 1 1 1 1 _ _ _ _ _ …

back_ 1 1 1 1 _ _ _ _ _ …

back_ 1 1 1 1 _ _ _ _ _ …

back_ 1 1 1 1 _ _ _ _ _ …

back_ 1 1 1 1 _ _ _ _ _ …

done_ 1 1 1 1 _ _ _ _ _ …

Wow! 2+1=3

( 1 , 1 , ) go d1 d2 back

( _ , _ , )

( 0 , 1 , )

( 1 , _ , )( 1 , _ , )

( 1 , 1 , )

( _ , _ , )

done

• Bit: a binary digit denoting

• Numeric value (1 or 0),

• Boolean value (true or false), or

• Voltage (high or low)

• Byte: a string of 8 bits.

• Most significant bit

• The first bit at the high-order end (leftmost)

• Least significant bit

• The last bit at the low-order end (rightmost)

Bits

For example,

this desktop computer has

• 64-bit Intel Core 2 Duo E4400 Processor,

• 2.0GHz, 2MB L2 cache

• 1GB DDR2 SDRAM at 667MHz, and

• 250 GB SATA hard drive.

Bits and Bytes

Data in The Computer

Source: http://homepage.cs.uri.edu/faculty/wolfe/book/Readings/Reading02.htm

16

• How are data represented inside a computer?

• Number coding system: Decimal vs. binary

• How do computers manipulate such data?

• Numbers

• Addition/subtraction

• Multiplication/division

• Shift

• Alphabets

• Boolean

• Etc.

Today’s Topics

1

1

?+

17

• Numeration symbols on tortoise shells and flat

cattle bones (or oracle bones).

• Based on a decimal system

• Positional value system

• Shang numerals 1-9 are shown on the top right.

Numeration Symbols in Ancient China

• They are simple to work with

• no big addition tables and multiplication tables to learn, just do the same things over and over, very fast.

• They just use two values of voltage, magnetism, or other signal, which makes the hardware easier to design and more noise resistant.

Why Use Binary Numbers?

• The transistor is a solid state semiconductor device, which changes its

conduction under the control of an electric field.

• Usage: amplification, switching, voltage stabilization, signal modulation

and many other functions.

• There are three terminals.

• In digital circuits, transistors function essentially as electrical switches.

There are two types

• bipolar junction transistor (BJT)

• field-effect transistor (FET)

Source: German wikipedia

Transistors

20

• The (0,1,…,9) world is called the decimal system.

• The (0,1) world is called the binary system.

Decimal vs. Binary

21

11

10

+

101

11

+

111

100

+

100101

1001

+

11

2

+

21

3

+

31

4

+

45

9

+

Binary Addition

How much is 100 = ?100 = 4 or 1100100? It depends!

23

• In decimal system

• 0, 1, 2, 3… 9999 (104)

• In Turing machine

• 1, 11, 111, 1111

• Translated: 0, 1, 2, 3

• In binary system

• 0, 1, 10, 11, …, 1111

• Translated: 0, 1, 2, 3, …, 15 (24)

_ _ _ __

If you have only 4 slots

24

0 0 1 0

103 102 101 100Our world

23 22 21 20Computer’s world

The Meaning of 10

25

0 1 0 0

103 102 101 100Our world

23 22 21 20Computer’s world

100 in binary system = 4 in decimal system

The Meaning of 100

26

From Binary to Decimal

27

1 1 1 1

23 22 21 20

8 4 2 1

+++ = 15

Convert Binary to Decimal

28

0 1 0 0

26 25 24 23 22 21 20

64 32 16 8 4 2 1

1 1 0

64+32+ 4 = 100

1100100 in binary system = 100 in decimal system

Another Example

29

From Decimal to Binary

30

!

13 = A " 23

+ B " 22

+ C " 2 + D

An Example

31

27 26 25 24 23 22 21 20

128 64 32 16 8 4 2 1

0 1 0 01 1 0

X 100

36X

4X

0

Convert Decimal to Binary

32

28 27 26 25 24 23 22 21 20

256 128 64 32 16 8 4 2 1

X 200

72X

8X

0

0 1 0 01 1 0 0

Another Example

33

0 1 0 01 1 0 0

0 1 0 01 1 0 100

200

0 1 0 01 1 0 0 0 ? 400

Notice Something?

34

0 1 0 01 1 0 0

0 1 0 01 1 0 1,100,100

11,001,000

0 1 0 01 1 0 0 0 110,010,000

x 10

x 10

In Decimal System

35

0 1 0 01 1 0 0

0 1 0 01 1 0 100

200

0 1 0 01 1 0 0 0 400

x 2

x 2

In Binary System

36

9-bit machine

…_ 1 1 0 0 1 0 0 0 1 1 0 0 1 0

200 100

0 00 0

_ _ _ __ _ _ _ _

Byte (8 bits)

bit

Bits and Bytes

• 2^10 is a K (kilo)

• 2^20 is a M (mega)

• 2^30 is a G (giga)

• 2^40 is a T (tera)

• 10^3 is a k (also kilo)

• 10^6 is a m (mega)

• 10^9 is a g (giga)

• 10^12 is a t (tera)

What is 40GB?

40 gigabytes = 40 x 2^30 x 8 bits

Units

38

Let n be represented by binary numbers

200 100

+

300

200+100=300

+

Cool

Add to Two,

Carry One

_ 1 1 0 0 1 0 0 0 1 1 0 0 1 0

200 100

0 00 0

Try 200+100?

39

_

_

+

_ _ _ _

_ Carry

Z_

0 1 1 0 0 1 0 Y0

1 1 0 0 1 0 0 X0

__

_

_

_

_

_

__

8 possibilities

0

0

1

0

1

0

0

0

1

0

0

1

0

1

1

1

0

1

0

1

input output

X Y C Z C

0 0 0

0 0 1

0 1 0

0 1 1

1 0 0

1 0 1

1 1 0

1 1 1

0 0

1 0

1 0

0 1

1 0

0 1

0 1

1 1

300!

0

0

0

0

The State Machine• Imagine a Turing machine with four tapes.

This is exactly how your PC adds two numbers up!

almost

What non-negative integers can be represented in n bits?

0 ~ 2n-1

Most straightforward

• 0 000

• 1 001

• 2 010

• 3 011

• 4 100

• 5 101

• 6 110

• 7 111

How about?

• 7 000

• 6 001

• 5 010

• 4 011

• 3 100

• 2 101

• 1 110

• 0 111

The mapping between a number and the corresponding representation inside the computer.

3+4=?

100+ 011

Coding Scheme

• Given a 4-bit binary system

• Number 1 is 0001

• Idea: use a sign-bit

• Signed Magnitude

• 3-bit left for numbers

• Number 1

• Number -1

• How about 1+ (-1) = ?

43

0 0 0 1

1 0 0 1

How do we represent -1?

44

• To negate a number, replace all zeros with ones, and ones with zeros - flip the bits.

• Example:

• 12 would be 00001100

• -12 would be 11110011

• As in signed magnitude, the leftmost bit indicates the sign (1 is negative, 0 is positive).

• To compute the value of a negative number, flip the bits and translate as before.

1’s Complement

45

! Given any binary number, create its negation with the two-step process:

• Flipping all the bits

• Adding 1 to the result

• No sign bit

• Example: 19 in decimal

00000000000100112

!1111111111101100

!1111111111101101

2’s Complement

46

• Left-to-right vs. right-to-left in written languages

• Big endian

• The most significant byte (MSB) is stored at the memory location with the lowest address.

• Little endian

• the least significant byte (LSB), I.e. the little end goes in first

• E.g. Intel CPUs use the little endian system, while Motorola CPUs use the big endian

Endian

• Overflow

• Happens when a number is too big to be represented

• Truncation

• Happens when a number is between two representable numbers

• Think the real numbers..

Limitations of Binary Numbers

48

• Why??

• Integers have limited usefulness.

• Size limitation

• Decimal fraction

• Computer version of scientific notation

• Exponent

• Fraction

• Example:

• 0.75 X 102

• Binary: .1001011 X 2111

Floating-Point Numbers

• Printable character

• Letter, punctuation, etc.

• Assigned a unique bit pattern.

• ASCII

• 7-bit values

• For most symbols used in written English text

• Unicode

• 16-bit values

• For most symbols used in most world languages

• ISO

• 32-bit values

Representing Text

50

• ASCII: the American Standard Code for Information Interchange

• ASCII uses eight bits (a single byte) for each character, thus allowing 256 different characters.

• Numbers

• Letters

• Special symbols

• For example, the upper case letter A has the ASCII code 01000001.

Characters

51

ASCII - “Hello.”

52

• Why?

• Things are in bit streams

• Stream = a long string of bits.

• Long bit streams

• E.g., 100 = 11001002

• Hexadecimal notation

• A shorthand notation for streams of bits.

• More compact.

Hexadecimal Notation

The Hexadecimal System

54

• A single 8-bit byte doesn’t offer enough combinations to represent distinct characters in Chinese

• Solution: use double bytes

• Problem: there are many alternative character encoding schemes

• Chinese Traditional (Big-5)

• Chinese Simplified (GB2312)

• Unicode (UTF-8)

• Etc.

Chinese Characters

55

• Boolean operation

• any operation that manipulates one or more true/false values

• Can be used to operate on bits

• Specific operations

• AND

• OR

• XOR

• NOT

Boolean Operations AND, OR, XOR

57

• Gates

• devices that produce the outputs of Boolean operations when given the operations’ input values

• Often implemented as electronic circuits

• Provide the building blocks from which computers are constructed

Gates

58

• Bitmap techniques

• Points, pixels e.g. !"#$%

• Representing colors

• BMP, DIB (Device Independent Bitmap)

• Compressed format

• e.g. GIF (Graphic Interchange Format) and JPEG

• Scanners, digital cameras, computer displays

• Vector techniques

• Lines, scalable fonts, e.g., TrueType

• Format: e.g. SVG (Scalable Vector Graphics)

• PostScript, Acrobat PDF

• CAD systems, printers

Representing Images

60

Representing Sound

62

Audio

Questions?No question is dumb.