This preview shows page 1. Sign up to view the full content.
Unformatted text preview: Engineering 101
Engineering 101 Data Types Bonus Comic!
Bonus Comic! Quote of the Day
Quote of the Day
Judge people by their questions
rather than by their answers.
Voltaire Two Important Halves of Two Important Halves of Computer Programming Execution Control
Sequence Selection Iteration Data Representation How do we structure the data that we are acting on? Data Types
Data Types Definition a set of possible values plus, a set of legal operations on those values For example (int): Integers within a given range Operations: addition (+), subtraction (), etc. MixedType Expressions
MixedType Expressions Even when binary operators are defined for operands of the same type, you can often apply them to different (mixed) types.
For example, arithmetic operators:
53 + 26.2 53 / 26.2 26.2 true To evaluate, the more restricted operand is promoted to type of the other
bool → int → double Type Coercion
Type Coercion Coercion: The implicit (automatic) conversion of a value from one data type to another Coercion may apply when: Evaluating a mixedtype expression
Assigning a value promoted to 3.0
double myDub = 3;
int myInt = 24.9;
demoted to 24
double avg = 39/5; promoted to ?? Passing a value to a routine
Returning a value from a function Type Casting
Type Casting Cast: The explicit conversion of a value from one data type to another Syntax of cast operation: T(expr) Evaluates expr, converts to type T int(24.9)
int(x+0.5)
double(39)/5 24 (just takes whole part)
rounds to nearest int (if x positive)
7.8 Explicit cast often recommended for clarity, even when coercion would apply Data Representation
Data Representation All data inside the computer is represented by bits.
Bits can have only two values 0 or 1.
4 bits make a nibble
8 bits make a byte
11001010
10101011 Data Encoding
Data Encoding
Bytes
11001110
10101010
11110000
10100101
11111111
11001110
11001110 Meaning
Hail!
to
the
victors
valiant
Hail!
Hail! How many things can you encode How many things can you encode with a byte (8 bits)? Each bit allows two possibilities.
1 bit:
0
1
(2 things)
2 bits: 00 01 10 11
(4 things)
3 bits: 000 001 010 011 100 101 110 111
(8 things)
n bits:
2n things
8 bits:
256 things Base 2 is a number system like Base 2 is a number system like base 10
456,347 Base 2 is a number system like Base 2 is a number system like base 10
456,347 100000 10000 1000 100 10 1 105 104 103 102 101 100 4 5 6 3 4 7 Base 2 is a number system like Base 2 is a number system like base 10
456,347 100000
105
4 10000 1000
104 103 100 10 1 102 101 100 5
6
3
4
4 x 105 + 5 x 104 + 6 x 103 + 3 x 102 + 4 x 101 + 7 x 100 7 Base 2 is a number system like Base 2 is a number system like base 10
110001 32 16 8 4 2 1 25 24 23 22 21 20 1 1
0
0
0
1 x 25 + 1 x 24 + 0 x 23 + 0 x 22 + 0 x 21 + 1 x 20 1 Base 2 is a number system like Base 2 is a number system like base 10
110001 32 16 8 4 2 1 25 24 23 22 21 20 1 1
0
0
0
1
1 x 25 + 1 x 24 + 0 x 23 + 0 x 22 + 0 x 21 + 1 x 20 110001(base 2) = 32+16+1 = 49(base 10) Convert the Binary Number to Convert the Binary Number to Decimal 128 64 32 16 8 4 2 1 27 26 25 24 23 22 21 20 01011001 1 27 2 89
3 67 4 79 Convert the Binary Number to Convert the Binary Number to Decimal 128 64 32 16 8 4 2 1 27 26 25 24 23 22 21 20 01101011 1 85 2 101
3 97 4 107 Adding Binary Numbers
Adding Binary Numbers + 0 1 1 0 0
0 1 1 1 0 Adding Binary Numbers
Adding Binary Numbers
0 1 1 0 0
+ 0 1 1 1 0 0 numbers are added column by column Adding Binary Numbers
Adding Binary Numbers
0 1 1 0 0
+ 0 1 1 1 0 1 0 numbers are added column by column Adding Binary Numbers
Adding Binary Numbers
0 1 1 0 0
+ 0 1 1 1 0 2 1 0 2 is not a valid binary digit.
In this case recall that 10 in binary equals 2 in decimal Adding Binary Numbers
Adding Binary Numbers 1
0 1 1 0 0
+ 0 1 1 1 0 0 1 0 So we replace the 2 by a 0 and carry the extra 1. Adding Binary Numbers
Adding Binary Numbers 1
0 1 1 0 0
+ 0 1 1 1 0 0 1 0 Now what do we do with 1 + 1 + 1?
Recall that 11 in binary equals 3 in decimal Adding Binary Numbers
Adding Binary Numbers 1 1
0 1 1 0 0
+ 0 1 1 1 0 1 0 1 0 So we output a 1 and carry the extra 1. Adding Binary Numbers
Adding Binary Numbers 1 1
0 1 1 0 0 + 0 1 1 1 0 1 1 0 1 0 (8 + 4 = (8 + 4 + 2 = (16 + 8 + 2 = 12)
14)
26) Add the Binary Numbers
Add the Binary Numbers
0101 + 0010 = 1 0110
2 0111
3 1001
4 1010 Add the Binary Numbers
Add the Binary Numbers
0101 + 0101 = 1 0110
2 0111
3 1001
4 1010 Overflow in Binary Addition
Overflow in Binary Addition Typically we are limited to representing a number by a certain number of binary digits.
Let’s say we are using 4 bits and want to add 1110 and 0010
The answer is 10000, but we only have 4 bits so the extra 1 is lost and we reset to 0000
This is like the odometer on your car.
Such overflow can lead to funny unexpected results. Negative Binary Numbers
Negative Binary Numbers How do we encode negative numbers in binary?
Consider our 4 bit overflow problem.
1111 + 0001 = 0000 Also, 0000 – 0001 = 1111 This implies that 1111 in our 4 bit representation could stand for 1 Negative Binary Numbers
Negative Binary Numbers
(32 bit, 2’sComplement)
00000000000000000000000000000100 = 4
00000000000000000000000000000011 = 3
00000000000000000000000000000010 = 2
00000000000000000000000000000001 = 1
00000000000000000000000000000000 = 0
11111111111111111111111111111111 = 1
11111111111111111111111111111110 = 2
11111111111111111111111111111101 = 3
11111111111111111111111111111100 = 4 You can find the negative of a number by flipping all the bits and adding 1. Numeric Data Types
Numeric Data Types Much (but not all!) computing is about numbers There are multiple types of numbers All programming languages have special facilities to handle numeric data
They differ on the class of values that can be expressed Two basic C++ number types: int : an integer in restricted range
(typically around –2 billion to +2 billion)
double : number with fractional part, very large range of magnitudes Numeric Data Types in C++
Numeric Data Types in C++
number integral floating int
char
short
long
[ unsigned ] float
double
long double bool
enum Floating Point (in brief)
Floating Point (in brief) A binary version of scientific notation
64bit double uses:
1 bit for sign (+/–) 11 bits for exponent 52 bits for significand (mantissa) Despite the wide range of expressible numbers, in general the representation is approximate. Integral Types
Integral Types Integral types have discrete, ordered values
5 < 17 'A' < 'C' false < true Each value has a unique successor and predecessor in the data type (unless it is at the end of the range) What comes after 5? 6 Floating Types
Floating Types Floating types have ordered, but not discrete values
No unique successor and predecessors What comes after 5.0? Depends on precision of the computer you are using:
5.00001 ?
5.000001 ?
5.0000001 ??? C++ Integer Types
C++ Integer Types int standard integer
4 bytes –2147483648 to 2147483647 unsigned int cannot be negative
4 bytes
0 to 4294967295 char bool a character: ‘a’, ‘D’
1 byte
true or false
1 byte The char Type
The Represents an individual character by an integer.
We denote char literals by enclosing them in single quotes Example: 'F'
Special whitespace chars: spaces ' ', tabs '\t', newlines '\n' Character set: chars available on a particular computer and their numeric codes (thus, their order) ASCII: 1 byte, 128 characters, numbered 0 thru 127
Most commonly used unicode: 2 bytes, supports more int’l characters
Increasingly popular (used in Java) ASCII Encoding
ASCII Encoding Example: 'A' encoded as integer 65
'A' external representation
01000001
internal representation
Basic ordering:
blank < digits < uppercase < lowercase 32 4857 6590 97122 031 and 127 are “nonprinting” characters (e.g., '\n') Reading and Writing chars
Reading and Writing Characters can be read in from input streams and written to output streams char c;
cin >> c;
cout << c << endl;
Usually whitespace chars are ignored during input unless you specifically turn this feature off.
cin.unsetf(ios::skipws); More about ASCII
More about ASCII Letters are contiguous, as are digits The character after 'I' is 'J'
The character after '8' is '9'
This is not true in some character sets cast operation
char ch = 'A';
ch++;
cout << ch << int(ch); B66 'A' < 'C' // compared using codes
'}' > '$' Example: Convert Digit Char to Number
Example: Convert Digit Char to Number
(if possible)
In <ccttype> library
if (isdigit (ch)) num = int (ch '0');
else cerr << "error! not numeric"; • If ch is '5', then num ← 53 – 48 = 5
• Usage: Interactive I/O, checking
input data one character at a time Convert to Lower Case
Convert to Lower Case
char upper2lower (char ch)
{ if (( ch >= ‘A’ ) and ( ch <= 'Z' )) return ( char ( ch + 32 ) ); else return ( ch );
} Next Lecture
Next Lecture Static Variables / Scope / Lifetime ...
View Full
Document
 Fall '07
 JeffRingenberg
 Volt

Click to edit the document details