1 - Introduction - Fundamentos Teoria

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

Universidad de Oviedo Fundamentos de Informática

Unit 1

Introduction

Computer Science Fundamentals

University of Oviedo
University of Oviedo Computer Science Fundamentals

Table of contents

1.1 Overview of computer science


1.2 Structure and operation of a computer
1.3 Representation of information in a computer
1.3.1 Number representation systems: decimal, binary,
hexadecimal
Integer number representation
Real number representation
Character representation
Boolean representation
1.4 Propositional logic

Unit 1: Introduction 2
University of Oviedo Computer Science Fundamentals

1.1 Overview of computer science


• Informática: lnformación + automática (French origin)
o According to the RAE: A set of scientific knowledge and
techniques that allow us the automatic processing of information
by means of computers.
§ Science that deals with the acquisition, storage, representation,
processing and transmission of information using machines known
as computers.
o Information in companies is managed using different software
tools, including
§ Different operating systems
§ Networking
§ Databases
§ User applications
§ Applications for the control and
management of the production.

Unit 1: Introduction 3
University of Oviedo Computer Science Fundamentals

1.2 Structure and operation of a computer

• What is a computer?
It is a machine that collects data,
processes them, and produces
an output.
It processes information much
Data bus
quicker than the human brain.
Address bus
It is programmable.
Control bus
• How are these actions carried
Given a second order polynomial p(x)=a·x2+b·x+c,
out? evaluate p(x) for all numbers x other than zero
o Architecture and block provided by the user.

diagram Algorithm:
print("Evaluating polynomials")
print("p(x)=a*x*x+b*x+c")
a = float(input("Type a: "))
§ Unique address space Ask for the coefficients b = float(input("Type b: "))
Ask for x c = float(input("Type c: "))

o Program While x!=0 x = float(input("Type x: "))


while x!=0:
Calculate p(x) p = a*x*x+b*x+c
§ Algorithm Show p(x)
Ask for x
print("p(",x,")=",p)
x = float(input("Type x: "))
print("End of the program")

Unit 1: Introduction 4
University of Oviedo Computer Science Fundamentals

1.2 Structure and operation of a computer

• Information on a computer
o Electrical signals are exchanged
through the buses
o Electrical signals are discrete (ALL /
NOTHING)!
o One example: the old Motorola 68000

Memory

Buses

CPU

Unit 1: Introduction 5
University of Oviedo Computer Science Fundamentals

1.3 Representation of information in a computer

• In order to process information • Decimal system


in a computer it must be o Base 10
represented or encoded § Digits {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
o Computers only work with 1/0 o Decomposing a number into
à Binary information! digits (and weights): 2x10-2
• Binary system 3x10-1
o Base 2 138.32 8x100
3x101
§ Digits {0, 1} 2 1 0 -1 -2
1x102
§ Bit à binary digit (the basic
information unit) 138.32
o Groupings Broadly speaking, given a number abcd.ef
represented in base B, its conversion to the decimal
§ 8 bits à 1 Byte system is done by successive multiplications by
§ 103 bytes à kB powers of the base B, as follows:
§ 210 bits à kb abc.d à a*B2 + b*B1 + c*B0 + d*B-1
2 1 0 -1
Unit 1: Introduction 6
University of Oviedo Computer Science Fundamentals

1.3 Representation of information in a computer

• Hexadecimal system 10 0x 2 10 0x 2
o Base 16 0 0 0000 8 8 1000
§ Digits {0, 1, 2, 3, 4, 5, 6, 7, 8, 1 1 0001 9 9 1001
9, A, B, C, D, E, F} 2 2 0010 10 A 1010
o Possible values: integers in the 3 3 0011 11 B 1011
range [0, 15]
4 4 0100 12 C 1100
o Direct match between
5 5 0101 13 D 1101
hexadecimal and binary:
§ 16=24 à all the hex digits can 6 6 0110 14 E 1110
be represented with 4 bits 7 7 0111 15 F 1111
o Conversion between Use this table to easily convert
hexadecimal and decimal from binary to hexadecimal.
§ The same as in the binary case

Unit 1: Introduction 7
University of Oviedo Computer Science Fundamentals

Positive integer number representation

FROM BASE B TO BASE 10 FROM BASE 10 to BASE B


Broadly speaking, given a number abcd.ef Broadly speaking, given a number abcd.ef
represented in base B, its conversion to the represented in base 10, its conversion to base B is
decimal system is done by successive done by successive divisions by the base B, as
multiplications by powers of the base B, as follows: follows: 25/2=12, remainder 1
12/2=6, remainder 0
abc.d à a*B2+b*B1+c*B0+d*B-1
2 1 0 -1
6/2=3, remainder 0 à 110012
3/2=1, remainder 1
1/2=0, remainder 1

• We are going to use three different systems:


Decimal Binary Hexadecimal
Used by humans Used by the CPU Intermediate representation

Base B=10 B=2 B=16


Digits 0 1 2 3 4 5 6 7 8 9 01 0123456789ABCDEF

Unit 1: Introduction 8
University of Oviedo Computer Science Fundamentals

Positive integer number representation

• From decimal to binary


o 18 à 18/2=9 r=0; 9/2=4 r=1; 4/2=2 r=0; 2/2=1 r=0; 1/2=0 r=1 à 10010
o 29 à 29/2=14 r=1; 14/2=7 r=0; 7/2=3 r=1; 3/2=1 r=1; 1/2=0 r=1 à 11101
• From binary to decimal
o 11010 à 1*24+1*23+0*22+1*21+0*20=26 à 26
o 0110111 à 0*26+1*25+1*24+0*23+1*22+1*21+1*20=55
• From binary to hexadecimal (8 bits)
o 0011010 à 0001 1010 à 0x1A
o 0110111 à 0011 0111 à 0x37
• From hexadecimal to decimal
o 1A2F à 1*163+10*162+2*161+15*160=6703 à 6703
• From hexadecimal to binary
o 1A2F à 0001 1010 0010 1111

Unit 1: Introduction 9
University of Oviedo Computer Science Fundamentals

Signed integer and real numbers

• Signed integer numbers • Real numbers (floating


o As in the unsigned case but point representation)
using the most significant bit for o Standard IEEE 754
the sign +(0) o –(1). o Use of scientific notation plus
o In two’s complement some simplification rules
Example to represent -50: o Using 32 or 64 bits

1.- We represent 50 in binary: Sign Exponent Mantissa


50 à 00110010 1 bit 8 or 11 bits 23 or 52 bits
2.- We get the two’s complement of 50
(we invert the number and add 1)
00110010 à 11001101 à 11001110

Unit 1: Introduction 10
University of Oviedo Computer Science Fundamentals

Characters and boolean data representation

• Characters • Boolean data


o Alphanumeric, punctuation o Possible values True or False
marks, etc. (1 or 0)
o Encoding table o Named in honor of George
Boole (1815-1864), father of the
§ ASCII & extended ASCII propositional logic.
o Examples that produce a
Boolean value:
o 3 > 5 à False
o 14 >= 2 à True
o 3 is an integer à True
o A Boolean value determines the
minimum possible amount of
information: 1 bit

Unit 1: Introduction 11
University of Oviedo Computer Science Fundamentals

System of units in computer science


• According to the International System of Units (SI):

Kilobit (kb) 103 bits Kilobyte (kB) 103 bytes


Megabit (Mb) 106 bits = 1000 kb Megabyte (MB) 106 bytes = 1000 kB
Gigabit (Gb) 109 bits = 1000 Mb Gigabyte (GB) 109 bytes = 1000 MB
Terabit (Tb) 1012 bits = 1000 Gb Terabyte (TB) 1012 bytes = 1000 GB
Petabit (Pb) 1015 bits = 1000 Tb Petabyte (PB) 1015 bytes = 1000 TB
Multiples of a bit Multiples of a byte

Unit 1: Introduction 12
University of Oviedo Computer Science Fundamentals

1.4 Propositional logic


• Logic is what will allow us to take decisions in a computer program
• It models human reasoning in a way suitable to be understood by a
machine
• It is vital to handle the main ideas behind logic to be able to write
programs correctly
• Real life example: Airplane landing
o When a plane lands it needs to reduce speed before wheel brakes can be
applied
o Depending on the runway length and other parameters, it is required to use
the reverse thrust
o Reverse thrust can only be used when the plane “knows” that all the whole
landing gear has touched ground

“IF all_wheels_are_on_the_ground AND pilot_engages_reverse


THEN engage_reverse_thrust”

Unit 1: Introduction 13
University of Oviedo Computer Science Fundamentals

Boolean values and operators

• A truth, logical or Boolean value can only be True


(T) or False (F)
• A logical expression produces a truth value, just like
an arithmetic expression returns a number
• Logical expressions are built up using two types of
operators
o Relational operators: ==, !=, <=, >, etc.
o Boolean connectives: AND, OR, NOT

Unit 1: Introduction 14
University of Oviedo Computer Science Fundamentals

Semantic rules

• Being P1 and P2 two parts of a logical expression, known


as propositions, the following semantic rules for the
connectives apply:

P1 P2 P1 Ú P2 P1 ¬ P1
T T T T T T T F
T F F T F T F T
F T T NOT
F T F
F F F F F F

AND OR

TRUTH TABLES FOR THE THREE CONNECTIVES

Unit 1: Introduction 15
University of Oviedo Computer Science Fundamentals

Evaluation of logical expressions

• Evaluation of (x>3) Ù ¬ (y<=0) • Evaluation of


with {x= 4, y= 5} ((x>3) Ù ¬(y<=0)) Ú A
with {x= 4, y= 5, A=T}

(x>3) Ù ¬ (y<=0) ((x>3) Ù ¬ (y<=0)) Ú A


1º (4>3) Ù ¬ (5<=0) 1º ((4>3) Ù ¬ (5<=0)) Ú A
2º T F 2º T F T
3º T T 3º T T T
4º T 4º T T
5º T

Unit 1: Introduction 16
University of Oviedo Computer Science Fundamentals

Logical laws
Logical laws are very important equivalences between propositions; i.e.,
propositions with an identical truth table. Being P, Q and R propositions:

• Associative • Complements
(P Ú Q) Ú R = P Ú (Q Ú R) P Ú ¬ P = T,
(P Ù Q) Ù R = P Ù (Q Ù R) P Ù¬P =F
• Commutative
PÚQ=QÚP • T and F
PÙQ=QÙP TÚP=T TÙP=P
FÚP=P FÙP=F
• Distributive
(P Ú Q) Ù R = (P Ù R) Ú (Q Ù R)
• De Morgan
(P Ù Q) Ú R = (P Ú R) Ù (Q Ú R)
¬(P Ú Q) = ¬ P Ù ¬Q
• Idempotent ¬(P Ù Q) = ¬ P Ú ¬Q
¬ ¬P = P

Unit 1: Introduction 17
University of Oviedo Computer Science Fundamentals

Syntactic simplifications

• Propositions can be simplified using the logical laws. Two examples:

¬ ((¬ A Ù B) Ú ¬(B Ú ¬ A)) =


¬ (¬ A Ù B) Ù ¬ ¬ (B Ú ¬ A) =
(¬ ¬ A Ú ¬ B) Ù (B Ú ¬ A) =
(A Ú ¬ B) Ù (B Ú ¬ A)

¬ ((x<=3 Ú x>=5) Ú (y<3 Ú y>=9)) =


¬ (x<=3 Ú x>=5) Ù ¬ (y<3 Ú y>=9) =
(¬ (x<=3) Ù ¬ (x>=5)) Ù (¬ (y<3) Ù ¬ (y>=9)) =
(x>3 Ù x<5) Ù (y>=3 Ù y<9)
Using interval notation: x ∈ (3,5) and y ∈ [3, 9)

Unit 1: Introduction 18
University of Oviedo Computer Science Fundamentals

Translations

• The human language can be translated into logical expressions.


Two examples:

- It is white, and it is not white


Being A = to be white, the translation is: A Ù ¬ A = F

- Being x and y integer numbers, at least one of them


is greater or equal to zero and the first one is not
greater than the second one
((x>=0) Ú (y>=0)) Ù ¬ (x>y) = ((x>=0) Ú (y>=0)) Ù (x<=y)

Unit 1: Introduction 19
University of Oviedo Computer Science Fundamentals

Exercises (taken from an exam)

• Consider the expression “either the number x is odd, or even and greater or equal
to 10”. Write a Boolean expression in Python using a variable x with the same
meaning as in the previous sentence.
(Note that the expression a%b in Python returns the remainder when a is divided by b)

(x%2 == 1) or ((x%2 == 0) and (x>=10))

• Negate the previous expression and then apply the De Morgan’s laws, writing the
result without any negations. What would be the result for x=6?

not ((x%2 == 1) or ((x%2 == 0) and (x>=10))) =


not (x%2 == 1) and not ((x%2 == 0) and (x>=10)) =
not (x%2 == 1) and (not(x%2 == 0) or not(x>=10)) =
(x%2 != 1) and ((x%2 != 0) or (x<10))
The result for x=6 is True
Unit 1: Introduction 20

You might also like