46
Foundations of Computer Science Computing …it is all about Data Representation, Storage, Processing, and Communication of Data 03/22/22 1 CS 112 – Foundations of Computer Science

Foundations of Computer Science Computing …it is all about Data Representation, Storage, Processing, and Communication of Data 10/4/20151CS 112 – Foundations

Embed Size (px)

Citation preview

Foundations ofComputer Science

Computing …it is all about Data Representation, Storage, Processing, and Communication of Data

04/19/23 1CS 112 – Foundations of Computer Science

A Computational Machine What capabilities should a computing machine have ? Well..any computational problem involves two primary

elements:

Data (numbers, text strings, or images, ...) used for input, manipulation and output

Instructions (that describes the operations (e.g. arithmetic or comparison) that must be performed on data pertinent to the problem being solved

Von Neumann – provided the insight that both data and instructions could be stored together in the computer

04/19/23 2CS 112 – Foundations of Computer Science

A Computational Machine Thus, the machine must have the following capabilities:

Representation & Storage (of both Data, and the Instructions that defines operations on data, why?)

Data Processing (interpret Instructions and carry out operations defined by these instructions on Data)

Data input and Data output (I/O) (why?)

Data Communication (optional, if the machine is connected to other similar machines – via a network)

04/19/23 3CS 112 – Foundations of Computer Science

Computer Capabilities A transistor can be set to either on (1) or off (0) by controlling the

direction of the electric current passing through it

Hundreds of Millions of transistors can be packed into a small ‘compact’ unit called the integrated circuit (IC) (a.k.a chips)

Transistor is said toTransistor is said tobe storing a 1 herebe storing a 1 here

5 Volts5 Volts==

1 is stored1 is stored

Power sourcePower source

resistanceresistance

Electric currentElectric current

transistortransistor

Transistor is said to Transistor is said to be storing a 0 herebe storing a 0 here

0 Volts0 Volts==

0 is stored0 is stored

Power sourcePower source

No resistanceNo resistance

Electric currentElectric current

transistortransistor

04/19/23 4CS 112 – Foundations of Computer Science

Computer Capabilities“Information in the Computer is bits + context”

Both data and instructions are encoded in the computer using Bits (Binary digits)

Think of a Bit as a code that can be either equal to 0 or 1

Encoding means that for each data value or algorithm instruction you use a code consisting of one or more bits to represent it.

For example, Street Traffic lights is a form of encoding for instructions to motorists (red = ‘stop’, yellow = ‘slow down’, green = ‘go’)

In computers bits are grouped together in 8-bit chunks called Bytes (e.g. 00101010, 11111111, and 00000000)

04/19/23 5CS 112 – Foundations of Computer Science

Computer Capabilities

Representation the encoding data using bits, and the encoding instructions using bits

Storage storing bits used to encode data and instructions

Data Processing for each instruction: 1) Interpreting the bits of the instruction,

and 2) doing the operation specified in the instruction on the bits of the data

specified in the instruction

04/19/23 6CS 112 – Foundations of Computer Science

Computer Capabilities

Data Input converting data transferred into the machine (say through a keyboard or mouse) into bits (and storing these bits)

Data Output converting bits of data into appropriate format

for transfer outside the machine (e.g. to the screen)

Data Communication Transfer bits of data from one computer to another

04/19/23 7CS 112 – Foundations of Computer Science

Computer Capabilities What are some of the different types of data that may be

encountered?

Text (sequences of characters used to represent names, descriptions, etc.)

Numeric (to represent different quantities, e.g. age, length, height, amount of money, growth rates)

Image (to represent photographic or synthesized pictures)

Audio (to represent audio signals of humans and otherwise)

Video (to represent video signals)

04/19/23 8CS 112 – Foundations of Computer Science

Computer Capabilities What are some of the different types of instructions that may be

used on this data?

Text (extracting characters, Concatenating characters together, comparing characters)

Numeric (arithmetic +, -, *, , comparison among other operations)

Audio (manipulate pitch, volume, high or low frequencies in an audio signal)

Image (manipulate pixels properties, e.g. color)

04/19/23 9CS 112 – Foundations of Computer Science

Data Representation & Storage

Representation by encoding works as follows:

First you have a value set; a fixed set of values you want to encode (data values likes numbers, characters, instructions to motorists,etc.).

You have a symbol set; a fixed set of symbols to use in constructing codes for these values (for computers this is generally the symbols {0,1}).

Using combinations of the symbols found in the symbol set you construct a unique code corresponding to each value in the value set.

04/19/23 10CS 112 – Foundations of Computer Science

Data Representation & Storage

Exercise IExercise I

To save Money a new traffic light device will be deployed To save Money a new traffic light device will be deployed where only two colors are available where only two colors are available redred && greengreen;; propose propose an encoding schemean encoding scheme that allows the that allows the stopstop, , slow downslow down, and , and gogo instructions to be given to motorists using this new instructions to be given to motorists using this new traffic light devicetraffic light device

AnswerAnswer

What’s the value set ?What’s the value set ?

What’s the symbol set?What’s the symbol set?

What’s the encoding scheme?What’s the encoding scheme?

04/19/23 11CS 112 – Foundations of Computer Science

Data Representation & Storage

Exercise IExercise I

To save Money a new traffic light device will be deployed To save Money a new traffic light device will be deployed where only two colors are available where only two colors are available redred & & greengreen;; propose propose an encoding schemean encoding scheme that allows the that allows the stopstop, , slow downslow down, and , and gogo instructions to be given to motorists using this new instructions to be given to motorists using this new traffic light devicetraffic light device

AnswerAnswer

What’s the value set ? What’s the value set ? stop, slow down & gostop, slow down & go

What’s the symbol set? What’s the symbol set? redred & & greengreen What’s the encoding scheme? What’s the encoding scheme? stop = stop = redred,, go = go = greengreen &&

slow down =slow down = redred && greengreen

04/19/23 12CS 112 – Foundations of Computer Science

Numeric Representation

What does 943 mean? As a decimal number

04/19/23 13CS 112 – Foundations of Computer Science

Numeric Representation

What does 943 mean? As a decimal number

9 * 102 = 9 * 100 = 900

+ 4 * 101 = 4 * 10 = 40

+ 3 * 100 = 3 * 1 = 3

943

called positional notation

here represented in base 10

04/19/23 14CS 112 – Foundations of Computer Science

Binary Representation

Generally speaking binary representation (base 2) of integers will use the symbols 0 & 1 in a positional representation where positions represent powers of 2.

24 23 22 21 20 or 16 8 4 2 1

thus 11001 = 16 + 8 + 0 + 0 + 1 = 25

04/19/23 15CS 112 – Foundations of Computer Science

Binary Representation Represent the decimal number 125 in binary The standard algorithm

(1) Start with your number, here 125, in base 10

(2) Divide the number (125) by 2 and record the remainder

125 / 2 has a quotient of 62 and a remainder of 1

(3) If the quotient = 0 stop,

else

Go to step 2 and repeat using the quotient as the number

(4) Record the remainders recorded from right to left

04/19/23 16CS 112 – Foundations of Computer Science

Binary Representation

Q R

125 / 2 62 1

62 / 2 31 0

31 / 2 15 1

15 / 2 7 1

7 / 2 3 1

3 / 2 1 1

1 / 2 0 1

12510 = 11111012

04/19/23 17CS 112 – Foundations of Computer Science

Binary Addition

How did we learn to add?

?? Do you remember the tables ??

0 + 0 = 0

0 + 1 = 1

1 + 0 = 0

1 + 1 = 10

carry (hmmm… why didn’t they

give us this in grade school?)

04/19/23 18CS 112 – Foundations of Computer Science

Binary Addition

Example

1 1 1 1 1 carry

1 0 1 1 1 0

1 1 0 1 1

1 0 0 1 0 0 1

04/19/23 19CS 112 – Foundations of Computer Science

Negative NumbersSign and Magnitude

It's what we're used to: +17 -45

Fortunately there are only two choices for the sign: + and -

So why no add a bit, and then use

0 for + and 1 for -

Thus

1 0100110 -> - 38

sign bit

04/19/23 20CS 112 – Foundations of Computer Science

magnitude

Negative Numbers

Among other things this leads to two zeros

0 0000000 = +0 and 1 0000000 = -0

that aren't really different numbers.

Try instead something called 2’s complement

04/19/23 21CS 112 – Foundations of Computer Science

Negative Numbers

Might think of as clock arithmetic

10

2

4

3

7

6

-8

-2

-1

-3

-5

-4

-6

-7

5

00010000

0010

0100

0011

0111

0110

1000

1110

1111

1101

1011

1100

1010

1001

0101

04/19/23 22CS 112 – Foundations of Computer Science

Addition and Subtraction

3 + 2 = 5 or 0011 + 0010 = 0101

00010000

0010

0100

0011

0111

0110

1000

1110

1111

1101

1011

1100

1010

1001

0101

04/19/23 23CS 112 – Foundations of Computer Science

Addition and Subtraction

7 - 4 = 3 or 0111 - 0100 = 0011

00010000

0010

0100

0011

0111

0110

1000

1110

1111

1101

1011

1100

1010

1001

0101

04/19/23 24CS 112 – Foundations of Computer Science

Addition and Subtraction

-7 + 4 = -3 or 1001 + 0100 = 1101

00010000

0010

0100

0011

0111

0110

1000

1110

1111

1101

1011

1100

1010

1001

0101

04/19/23 25CS 112 – Foundations of Computer Science

Negative Numbers

2’s Complement Take the 1’s complement and add 1

5 -> 0101 1’s comp -> 1010

add 1 1

1011 -> -5 Now go the other way

-5 -> 1011 1’s comp -> 0100

add 1 1

0101 -> 5

Thus the 2’s complement of a 2’s complement is the original number (same is true for the negative)

04/19/23 26CS 112 – Foundations of Computer Science

Negative Numbers

Find -56 using an eight bit representation

56 -> 00111000

1's comp 11000111

add 1 1

11001000 -> -56

What does 10111001 represent?

2's complement it 01000110

1

01000111 -> 71

so the original number must have been -71

04/19/23 27CS 112 – Foundations of Computer Science

Negative Numbers

Find -83 using an eight bit representation

What does 10010101 represent?

What does 11111111 represent?

04/19/23 28CS 112 – Foundations of Computer Science

Need More Bits

Positive Numbers – Fill new positions to the left with 0

7 in four bits = 0111

7 in eight bits = 0000 0111

7 in sixteen bits = 0000 0000 0000 0111

Negative Numbers – Fill new positions to the left with 1

-7 in four bits = 1001

-7 in eight bits = 1111 1001

-7 in sixteen bits = 1111 1111 1111 1001

04/19/23 29CS 112 – Foundations of Computer Science

Addition and Subtraction

Addition – follow the usual rules 5 + 2 = 7 -4 + 5 = 1

0101 1100

0010 0101

0111 10001

carry out – ignored, it means

you passed 0

00010000

0010

0100

0011

0111

0110

1000

1110

1111

1101

1011

1100

1010

1001

0101

04/19/23 30CS 112 – Foundations of Computer Science

Addition and Subtraction

Subtraction can be represented as the addition of the negative

7 - 5 -> 7 + (-5)

0111 0111

- 0101 + 1011

10010

-4 - 3 -> -4 + (-3)

1100

1101

11001

04/19/23 31CS 112 – Foundations of Computer Science

Addition and Subtraction

So is there any concern?

Yes – no matter how many bits you use there is some maximum and some minimum number that can be represented.

For four bits those are +7 and -8 respectively.

5 + 6 = 11 and -6 + (-4) = -10 are beyond a 4-bit rep.

0101 1010

0110 1100

1011 10110

04/19/23 32CS 112 – Foundations of Computer Science

Addition and Subtraction

So is there any concern?

Yes – no matter how many bits you use there is some maximum and some minimum number that can be represented.

For four bits those are +7 and -8 respectively.

5 + 6 = 11 and -6 + (-4) = -10 are beyond a 4-bit rep.

0101 1010

0110 1100

1011 10110

two positives added to

a negative

04/19/23 33CS 112 – Foundations of Computer Science

Addition and Subtraction

So is there any concern?

Yes – no matter how many bits you use there is some maximum and some minimum number that can be represented.

For four bits those are +7 and -8 respectively.

5 + 6 = 11 and -6 + (-4) = -10 are beyond a 4-bit rep.

0101 1010

0110 1100

1011 10110

two negatives added to

a positive

04/19/23 34CS 112 – Foundations of Computer Science

Addition and Subtraction

So is there any concern?

Yes – no matter how many bits you use there is some maximum and some minimum number that can be represented.

For four bits those are +7 and -8 respectively.

5 + 6 = 11 and -6 + (-4) = -10 are beyond a 4-bit rep.

0101 1010

0110 1100

1011 10110

Both cases are called overflow.

04/19/23 35CS 112 – Foundations of Computer Science

Addition and SubtractionCarry out the following – if there is overflow indicate that – if there is no

overflow interpret your answer in decimal form. Numbers are listed in 2's complement.

1001 0011 0111 1111 1010

0101 0110 0011 1000 0011

1001 0011 0111 1111 1010

- 0101 - 0110 - 0011 - 1000 - 0011

04/19/23 36CS 112 – Foundations of Computer Science

Addition and SubtractionCarry out the following – if there is overflow indicate that – if there is

no overflow interpret your answer in decimal form. Numbers are listed in 2's complement.

1001(-7) 0011 0111 1111 1010

0101(5) 0110 0011 1000 0011

1110(-2)

1001 0011 0111 1111 1010

- 0101 - 0110 - 0011 - 1001 - 0011

04/19/23 37CS 112 – Foundations of Computer Science

Addition and SubtractionCarry out the following – if there is overflow indicate that – if there is

no overflow interpret your answer in decimal form. Numbers are listed in 2's complement.

1001(-7) 0011(3) 0111 1111 1010

0101(5) 0110(6) 0011 1000 0011

1110(-2) 1001(X)

1001 0011 0111 1111 1010

- 0101 - 0110 - 0011 - 1001 - 0011

04/19/23 38CS 112 – Foundations of Computer Science

Addition and SubtractionCarry out the following – if there is overflow indicate that – if there is

no overflow interpret your answer in decimal form. Numbers are listed in 2's complement.

1001(-7) 0011(3) 0111(7) 1111 1010

0101(5) 0110(6) 0011(3) 1000 0011

1110(-2) 1001(X) 1010(X)

1001 0011 0111 1111 1010

- 0101 - 0110 - 0011 - 1001 - 0011

04/19/23 39CS 112 – Foundations of Computer Science

Addition and SubtractionCarry out the following – if there is overflow indicate that – if there is

no overflow interpret your answer in decimal form. Numbers are listed in 2's complement.

1001(-7) 0011(3) 0111(7) 1111(-1) 1010

0101(5) 0110(6) 0011(3) 1000(-8) 0011

1110(-2) 1001(X) 1010(X) 10111(X)

1001 0011 0111 1111 1010

- 0101 - 0110 - 0011 - 1001 - 0011

04/19/23 40CS 112 – Foundations of Computer Science

Addition and SubtractionCarry out the following – if there is overflow indicate that – if there is

no overflow interpret your answer in decimal form. Numbers are listed in 2's complement.

1001(-7) 0011(3) 0111(7) 1111(-1) 1010(-6)

0101(5) 0110(6) 0011(3) 1000(-8) 0011(3)

1110(-2) 1001(X) 1010(X) 10111(X) 1101(-3)

1001 0011 0111 1111 1010

- 0101 - 0110 - 0011 - 1001 - 0011

04/19/23 41CS 112 – Foundations of Computer Science

Addition and SubtractionCarry out the following – if there is overflow indicate that – if there is

no overflow interpret your answer in decimal form. Numbers are listed in 2's complement.

1001(-7) 0011(3) 0111(7) 1111(-1) 1010(-6)

0101(5) 0110(6) 0011(3) 1000(-8) 0011(3)

1110(-2) 1001(X) 1010(X) 10111(X) 1101(-3)

1001(-7) 0011 0111 1111 1010

1011(-5) - 0110 - 0011 - 1001 - 0011

10011(X)

04/19/23 42CS 112 – Foundations of Computer Science

Addition and SubtractionCarry out the following – if there is overflow indicate that – if there is

no overflow interpret your answer in decimal form. Numbers are listed in 2's complement.

1001(-7) 0011(3) 0111(7) 1111(-1) 1010(-6)

0101(5) 0110(6) 0011(3) 1000(-8) 0011(3)

1110(-2) 1001(X) 1010(X) 10111(X) 1101(-3)

1001(-7) 0011(3) 0111 1111 1010

1011(-5) 1010(-6) - 0011 - 1001 - 0011

10011(X) 1101(-3)

04/19/23 43CS 112 – Foundations of Computer Science

Addition and SubtractionCarry out the following – if there is overflow indicate that – if there is

no overflow interpret your answer in decimal form. Numbers are listed in 2's complement.

1001(-7) 0011(3) 0111(7) 1111(-1) 1010(-6)

0101(5) 0110(6) 0011(3) 1000(-8) 0011(3)

1110(-2) 1001(X) 1010(X) 10111(X) 1101(-3)

1001(-7) 0011(3) 0111(7) 1111 1010

1011(-5) 1010(-6) 1101(-3) - 1001 - 0011

10011(X) 1101(-3) 10100(4)

04/19/23 44CS 112 – Foundations of Computer Science

Addition and SubtractionCarry out the following – if there is overflow indicate that – if there is

no overflow interpret your answer in decimal form. Numbers are listed in 2's complement.

1001(-7) 0011(3) 0111(7) 1111(-1) 1010(-6)

0101(5) 0110(6) 0011(3) 1000(-8) 0011(3)

1110(-2) 1001(X) 1010(X) 10111(X) 1101(-3)

1001(-7) 0011(3) 0111(7) 1111(-1) 1010

1011(-5) 1010(-6) 1101(-3) 0111(7) - 0011

10011(X) 1101(-3) 10100(4) 10110(6)

04/19/23 45CS 112 – Foundations of Computer Science

Addition and SubtractionCarry out the following – if there is overflow indicate that – if there is

no overflow interpret your answer in decimal form. Numbers are listed in 2's complement.

1001(-7) 0011(3) 0111(7) 1111(-1) 1010(-6)

0101(5) 0110(6) 0011(3) 1000(-8) 0011(3)

1110(-2) 1001(X) 1010(X) 10111(X) 1101(-3)

1001(-7) 0011(3) 0111(7) 1111(-1) 1010(-6)

1011(-5) 1010(-6) 1101(-3) 0111(7) 1101(-3)

10011(X) 1101(-3) 10100(4) 10110(6) 10111(X)

04/19/23 46CS 112 – Foundations of Computer Science