34
Data types and their representation Jordi Cortadella Department of Computer Science

Data types and their representation Jordi Cortadella Department of Computer Science

Embed Size (px)

Citation preview

Page 1: Data types and their representation Jordi Cortadella Department of Computer Science

Data types and their representation

Jordi CortadellaDepartment of Computer Science

Page 2: Data types and their representation Jordi Cortadella Department of Computer Science

Outline

• Data representation

• Boolean expressions

• Real numbers

Introduction to Programming © Dept. CS, UPC 2

Page 3: Data types and their representation Jordi Cortadella Department of Computer Science

The memory

0 0 1 0 0 1 0 0 1 1 1 0 0 0 1 0 1 0 1 0 0 1 0 0 1 0 1 0 0 0 1 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 10 0 1 0 0 1 0 0 0 1 1 1 0 0 0 0 0 1 1 0 1 0 0 1 0 1 0 1 0 0 0 01 0 0 1 0 0 1 1 0 0 0 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 1 0 0 1 0 1 0 1 0 0 0 0 1 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 1 0 00 0 1 0 0 1 0 1 1 1 1 0 0 0 0 0 0 1 0 0 0 0 1 0 0 1 0 0 0 0 0 11 1 0 1 1 0 1 0 0 0 0 1 0 0 1 0 0 0 0 0 1 0 0 0 1 0 0 1 0 0 1 00 1 0 1 0 1 0 1 0 1 0 0 0 1 1 1 1 1 0 1 0 0 0 0 0 0 0 0 1 0 0 10 0 1 0 1 0 0 0 1 0 0 1 0 1 1 0 0 0 1 0 0 1 0 0 0 1 0 1 0 1 1 00 1 1 1 0 0 1 1 0 1 0 0 1 0 0 0 1 0 0 0 1 0 1 0 1 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 1 0 1 1 0 1 0 0 0 0 0 1 0 0 0 0 1 1 1 1 1 0 0 1 0 0 0 1 0 00 0 1 0 0 0 1 0 0 1 0 0 0 0 0 1 1 0 0 1 0 0 0 0 0 0 1 0 0 0 0 1

• • •

• • • Address

1036104010441048105210561060106410681072107610801084

byte

int

19

0

H e l lo w or l d

string(“Hello world”)

Introduction to Programming © Dept. CS, UPC 3

Page 4: Data types and their representation Jordi Cortadella Department of Computer Science

How much memory do we have?Our laptop has few Gbytes of memory:

Introduction to Programming © Dept. CS, UPC 4

Page 5: Data types and their representation Jordi Cortadella Department of Computer Science

Number representation

2100 10 1

1 7 base 10

325 5 1

3 21125

base 5

09 3 1

0 1227

281

base 3

04 2 1

0 118

116

0113264128

base 2

CCXVII Roman

Introduction to Programming © Dept. CS, UPC 5

Page 6: Data types and their representation Jordi Cortadella Department of Computer Science

1

Write the binary representation

Design a procedure that, given a number n, writes its binary representation (in reverse order).

// Pre: n 0// Writes the binary representation of n// in reverse order.void base2(int n);

04 2 1

0 118

116

0113264128

Write: 10011011

0 011011

n%2n/2

Introduction to Programming © Dept. CS, UPC 6

Page 7: Data types and their representation Jordi Cortadella Department of Computer Science

Write the binary representation// Pre: n 0// Writes the binary representation of n// in reverse order.

void base2(int n) { while (n > 1) { cout << n%2; n = n/2; } cout << n << endl;}

n cout217 1108 0

54 027 113 1

6 03 11 1

Introduction to Programming © Dept. CS, UPC 7

Page 8: Data types and their representation Jordi Cortadella Department of Computer Science

ExerciseDesign a procedure that, given a number n and a base b, writes the representation of n in base b(in reverse order).

// Pre: n 0, 2 b 9.// Writes the binary representation of n// in base b (in reverse order).void base(int n, int b);

Introduction to Programming © Dept. CS, UPC 8

Page 9: Data types and their representation Jordi Cortadella Department of Computer Science

Boolean expressions

Introduction to Programming © Dept. CS, UPC 9

Page 10: Data types and their representation Jordi Cortadella Department of Computer Science

Boolean Algebra

George Boole, 1815-1864

Introduction to Programming © Dept. CS, UPC 10

Page 11: Data types and their representation Jordi Cortadella Department of Computer Science

Maximum of three numbers// Returns max(a, b, c)int max3(int a, int b, int c);

a > banda > c

a

b

b > c

c

else

a,b,c

else

b,c

a >= band

a >= c

a

b c

else

b >= c

else

a,b,c

b,c

Introduction to Programming © Dept. CS, UPC 11

Page 12: Data types and their representation Jordi Cortadella Department of Computer Science

Maximum of three numbers// Returns max(a, b, c)int max3(int a, int b, int c) { if (a > b and a > c) { return a; } else { if (b > c) { return b; } else { return c; } }}

Choosebetweenb and c

a > banda > ca

b

b > c

c

else

a,b,c

else

b,c

Introduction to Programming © Dept. CS, UPC 12

Page 13: Data types and their representation Jordi Cortadella Department of Computer Science

Maximum of three numbers

// Returns max(a, b, c)int max3(int a, int b, int c) { if (a > b and a > c) return a; if (b > c) return b; return c;}

Introduction to Programming © Dept. CS, UPC 13

Page 14: Data types and their representation Jordi Cortadella Department of Computer Science

Boolean operators

if (a > b and a > c) …

while (i >= 10 or c == 0) …

if (not (a < b and a < c)) …

Introduction to Programming © Dept. CS, UPC 14

Page 15: Data types and their representation Jordi Cortadella Department of Computer Science

Truth tables

a b a and bfalse false falsefalse true falsetrue false falsetrue true true

a b a or bfalse false falsefalse true truetrue false truetrue true true

a not afalse truetrue false

Introduction to Programming © Dept. CS, UPC 15

Page 16: Data types and their representation Jordi Cortadella Department of Computer Science

Boolean expressions

-3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13

x > 2 and x < 7

-3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13

… …

x >= 3 and x <= 6

x <= 2 or x > 7

-3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13

…x <= 2 not (x <= 2) x > 2

-3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13

?x == 5

x

Introduction to Programming © Dept. CS, UPC 16

Page 17: Data types and their representation Jordi Cortadella Department of Computer Science

Boolean expressions

-3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13

x > 2 and x < 7

-3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13

… …

x >= 3 and x <= 6

x <= 2 or x > 7

-3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13

…x <= 2 not (x <= 2) x > 2

-3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13

not (x == 5) x != 5……

Introduction to Programming © Dept. CS, UPC 17

Page 18: Data types and their representation Jordi Cortadella Department of Computer Science

Exercise

-3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13

x == 0 or x == 6

Introduction to Programming © Dept. CS, UPC 18

Page 19: Data types and their representation Jordi Cortadella Department of Computer Science

Exercise

-3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13

x == 0 or x > 5

Introduction to Programming © Dept. CS, UPC 19

Page 20: Data types and their representation Jordi Cortadella Department of Computer Science

Exercise

-3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13

x%2 == 0

……

Introduction to Programming © Dept. CS, UPC 20

Page 21: Data types and their representation Jordi Cortadella Department of Computer Science

Exercise

-3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13

(x > -1 and x < 3) or(x >= 6 and x < 11)

Introduction to Programming © Dept. CS, UPC 21

Page 22: Data types and their representation Jordi Cortadella Department of Computer Science

Complement of and/or

-3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13

……

(x > 2 and x < 7)

not (x > 2 and x < 7)

x <= 2 or x >= 7

not not

De Morgan’s lawIntroduction to Programming © Dept. CS, UPC 22

Page 23: Data types and their representation Jordi Cortadella Department of Computer Science

De Morgan’s law

not (e1 and e2) not e1 or not e2

not (e1 or e2) not e1 and not e2

Exercise

Simplify: not (x >= y or y%2 == 0)

x < y and y%2 != 0

Introduction to Programming © Dept. CS, UPC 23

Page 24: Data types and their representation Jordi Cortadella Department of Computer Science

Operator precedence

a + bc (a + b)c

a or b and c (a or b) and c

Introduction to Programming © Dept. CS, UPC 24

Page 25: Data types and their representation Jordi Cortadella Department of Computer Science

Real numbers

Introduction to Programming © Dept. CS, UPC 25

Page 26: Data types and their representation Jordi Cortadella Department of Computer Science

Intersection of circles

x1, y1, r1 x2, y2, r2

• Write a program that reads the center and the radius of two circles and prints “yes” or “no” depending on whether they intersect or not.

• Example:

2 5.3 1.34 0 0 2 no 1.5 2.5 10 0.5 3.6 4.3 yes

Introduction to Programming © Dept. CS, UPC 26

Page 27: Data types and their representation Jordi Cortadella Department of Computer Science

Intersection of circles

(

(𝑟1 𝑟2

Circles intersect if and only if the distance betweencenters is smaller than or equal to the sum of radii.

Introduction to Programming © Dept. CS, UPC 27

Page 28: Data types and their representation Jordi Cortadella Department of Computer Science

Intersection of circles

(

(𝑟1 𝑟2

√ (𝑥1−𝑥2 )2+( 𝑦1−𝑦 2)2≤𝑟1+𝑟 2

(𝑥1−𝑥2 )2+( 𝑦1−𝑦 2 )2≤ (𝑟1+𝑟2 )2

Introduction to Programming © Dept. CS, UPC 28

Page 29: Data types and their representation Jordi Cortadella Department of Computer Science

// Reads the center and radius of two circles// and prints whether they intersect or not.int main() { double x1, y1, r1, x2, y2, r2; // Real numbers cin >> x1 >> y1 >> r1 >> x2 >> y2 >> r2;

double dx = x1 – x2; dx = dxdx; // (x1 – x2)^2 double dy = y1 – y2; dy = dydy; // (y1 – y2)^2 double r = r1 + r2; r = rr; // (r1 + r2)^2

bool intersect = dx + dy <= r; // true or false

if (intersect) cout << "yes" << endl; else cout << "no" << endl;}

Intersection of circles

Introduction to Programming © Dept. CS, UPC 29

Page 30: Data types and their representation Jordi Cortadella Department of Computer Science

Boolean variablesbool b = false;b = i <= n; // true if i <= n, false if i > nb = x + y > z;b = (i < 10) and (j >= 20);b = (large and not found) or (x > y);b = true;

if (b) { … } // if (b == true) { … }

while (not b) {…} // while (b == false) { … }

Introduction to Programming © Dept. CS, UPC 30

Page 31: Data types and their representation Jordi Cortadella Department of Computer Science

Real numbers• Two types:

– float (single precision, 32 bits)– double (double precision, 64 bits)

• Arithmetic operations: + - / (no remainder)

• Real constants:2 -5.003 3.1416 1.4e9 0.6E-15

1.4 0.6 Introduction to Programming © Dept. CS, UPC 31

Page 32: Data types and their representation Jordi Cortadella Department of Computer Science

© Dept. CS, UPC 32

Type conversion

• Arithmetic operations between integer and real values usually imply an implicit type conversion.

• Be careful:

int i=3, j=2; double x; x = i/j; // x = 1.0 x = i/double(j); // x = 1.5 x = double(i)/j; // x = 1.5 x = double(i/j); // x = 1.0 x = i/2; // x = 1.0 x = i/2.0; // x = 1.5

i = x; // i = 1 j = 3.14159265; // j = 3

Introduction to Programming

Page 33: Data types and their representation Jordi Cortadella Department of Computer Science

// Returns x2

double sq(double x) { return xx;}

// Reads the center and radius of two circles// and prints whether they intersect or not.int main() { double x1, y1, r1, x2, y2, r2; cin >> x1 >> y1 >> r1 >> x2 >> y2 >> r2;

if (sq(x1 – x2) + sq(y1 – y2) <= sq(r1 + r2)) cout << "yes"; else cout << "no";

cout << endl;}

Revisiting intersection of circles

Introduction to Programming © Dept. CS, UPC 33

Page 34: Data types and their representation Jordi Cortadella Department of Computer Science

Summary• Variables are stored in memory locations and

internally represented as bit vectors.

• Boolean expressions and variables:– Can take two values: true or false.– Use negative thinking when simpler than positive

thinking (apply De Morgan’s law).

• Real values:– Can be represented with a limited precision.– Be careful with int double conversions.

Introduction to Programming © Dept. CS, UPC 34