Data types and their representation Jordi Cortadella Department
- Slides: 34
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
The memory int • • • Address 1036 1040 1044 1048 1052 1056 1060 1064 1068 1072 1076 1080 1084 0 0 0 1 0 0 0 0 0 1 0 1 0 0 1 1 0 0 0 1 H 1 1 o 0 0 r 1 1 0 0 1 0 1 0 0 0 0 1 1 0 0 0 1 0 1 0 1 0 0 0 1 1 0 0 0 0 1 0 1 0 1 0 0 1 0 0 0 0 string (“Hello world”) Introduction to Programming 0 0 0 1 0 0 byte 0 1 0 0 0 1 e 0 0 1 1 l 0 1 0 0 0 1 0 0 1 1 1 0 0 0 1 0190 0 1 0 0 0 1 000 0 0 1 1 • • • © Dept. CS, UPC 0 0 1 0 1 0 0 1 0 0 0 l 1 1 w 0 0 d 0 0 1 1 1 0 0 0 0 1 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 1 0 1 0 0 0 0 1 l 0 0 o 1 1 0 0 0 0 1 0 1 1 0 0 0 1 0 0 1 3
How much memory do we have? Our laptop has few Gbytes of memory: Introduction to Programming © Dept. CS, UPC 4
Number representation 100 10 1 2 1 7 125 25 5 1 1 3 3 2 81 27 9 3 64 32 16 8 4 2 base 5 1 2 2 0 0 1 128 base 10 base 3 1 1 1 0 0 1 base 2 CCXVII Roman Introduction to Programming © Dept. CS, UPC 5
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 base 2(int n); 128 64 32 16 8 4 2 1 1 1 0 0 1 n/2 n%2 1 1 0 0 Introduction to Programming Write: 10011011 © Dept. CS, UPC 1 6
Write the binary representation // Pre: n 0 // Writes the binary representation of n // in reverse order. void base 2(int n) { while (n > 1) { cout << n%2; n = n/2; } cout << n << endl; } Introduction to Programming © Dept. CS, UPC n cout 217 1 108 0 54 0 27 13 6 3 1 1 1 0 1 1 7
Exercise Design 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
Boolean expressions Introduction to Programming © Dept. CS, UPC
Boolean Algebra George Boole, 1815 -1864 Introduction to Programming © Dept. CS, UPC 10
Maximum of three numbers // Returns max(a, b, c) int max 3(int a, int b, int c); a, b, c a > b and a > c a, b, c a >= b and a >= c else a b, c b > c b Introduction to Programming else a else c b, c b >= c b © Dept. CS, UPC else c 11
Maximum of three numbers // Returns max(a, b, c) int max 3(int a, int b, int c) { if (a > b and a > c) { return a; a, b, c } else { a > b if (b > c) { else and return b; Choose a > c } else { between return b and cc; a b, c } } b > c else } b c Introduction to Programming © Dept. CS, UPC 12
Maximum of three numbers // Returns max(a, b, c) int max 3(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
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
Truth tables a false true b a and b false true false true a false true b a or b false true true a not a false true false Introduction to Programming © Dept. CS, UPC 15
Boolean expressions x > 2 and x < 7 x >= 3 and x <= 6 x -3 -2 -1 0 1 2 4 5 6 7 8 9 10 11 12 13 x <= 2 or x > 7 … -3 -2 -1 … 3 0 1 2 3 0 5 6 7 8 9 10 11 12 13 not (x <= 2) x > 2 x <= 2 -3 -2 -1 4 … 1 2 3 4 5 6 7 8 9 10 11 12 13 … x == ? 5 -3 -2 -1 Introduction to Programming 0 1 2 3 4 5 6 © Dept. CS, UPC 16
Boolean expressions x > 2 and x < 7 x >= 3 and x <= 6 -3 -2 -1 0 1 2 4 5 6 7 8 9 10 11 12 13 x <= 2 or x > 7 … -3 -2 -1 … 3 0 1 2 3 0 5 6 7 8 9 10 11 12 13 not (x <= 2) x > 2 x <= 2 -3 -2 -1 4 … 1 2 3 4 5 6 7 8 9 10 11 12 13 not (x == 5) x != 5 … -3 -2 -1 Introduction to Programming 0 1 2 3 4 5 6 © Dept. CS, UPC 7 … … 8 9 10 11 12 13 17
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
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
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
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
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 not x <= 2 or x >= 7 De Morgan’s law Introduction to Programming © Dept. CS, UPC 22
De Morgan’s law not (e 1 and e 2) not e 1 or not e 2 not (e 1 or e 2) not e 1 and not e 2 Exercise Simplify: not (x >= y or y%2 == 0) x < y and y%2 != 0 Introduction to Programming © Dept. CS, UPC 23
Operator precedence a + b c (a + b) c a or b and c Introduction to Programming (a or b) and c © Dept. CS, UPC 24
Real numbers Introduction to Programming © Dept. CS, UPC
Intersection of circles • 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: x 1, y 1, r 1 x 2, y 2, r 2 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
Intersection of circles Circles intersect if and only if the distance between centers is smaller than or equal to the sum of radii. Introduction to Programming © Dept. CS, UPC 27
Intersection of circles Introduction to Programming © Dept. CS, UPC 28
Intersection of circles // Reads the center and radius of two circles // and prints whether they intersect or not. int main() { double x 1, y 1, r 1, x 2, y 2, r 2; // Real numbers cin >> x 1 >> y 1 >> r 1 >> x 2 >> y 2 >> r 2; double dx = x 1 – x 2; dx = dx dx; double dy = y 1 – y 2; dy = dy dy; double r = r 1 + r 2; r = r r; // (x 1 – x 2)^2 // (y 1 – y 2)^2 // (r 1 + r 2)^2 bool intersect = dx + dy <= r; // true or false if (intersect) cout << "yes" << endl; else cout << "no" << endl; } Introduction to Programming © Dept. CS, UPC 29
Boolean variables bool b = false; b = i <= n; // true if i <= n, false if i > n b = 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
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 Introduction to Programming © Dept. CS, UPC 1. 4 e 9 0. 6 E-15 31
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 = i/double(j); x = double(i)/j; x = double(i/j); x = i/2. 0; // // // i = x; j = 3. 14159265; // i = 1 // j = 3 Introduction to Programming x x x © Dept. CS, UPC = = = 1. 0 1. 5 32
Revisiting intersection of circles // Returns x 2 double sq(double x) { return x x; } // Reads the center and radius of two circles // and prints whether they intersect or not. int main() { double x 1, y 1, r 1, x 2, y 2, r 2; cin >> x 1 >> y 1 >> r 1 >> x 2 >> y 2 >> r 2; if (sq(x 1 – x 2) + sq(y 1 – y 2) <= sq(r 1 + r 2)) cout << "yes"; else cout << "no"; cout << endl; } Introduction to Programming © Dept. CS, UPC 33
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
- Jordi cortadella
- Numeric data representation
- Jordi reviriego
- Jordi ustrell
- Jordi benlliure
- Language telegram
- Jordi juanico sabate
- Jordi timmers
- Jordi ayala roqueta
- Jordi vives i batlle
- Jordi garcia cehic
- Jordi graells costa
- Jordi npa
- Jordi scene
- Jordi vives i batlle
- Dosmasuno arquitectos
- Jordi gisbert
- Dev23
- Ascii definition computer science
- Data representation and computer arithmetic
- Tabular and graphical representation of data
- Data representation and organization
- Interprocess communication in distributed systems
- Tabular and graphical representation of data
- Xdr external data representation
- Data representation computer science
- Machine level representation of data
- Information representation in computer architecture
- Assembly language
- Data representation in memory bit
- Data representation in assembly language
- Digital representation in multimedia
- Unit 2 data representation
- Is a graphical representation of data
- Data representation in memory