THE INTEGER DATA TYPES
STORAGE OF INTEGER TYPES IN MEMORY All data types are stored in binary in memory. The type that you give a value indicates to the machine what encoding to use to store the data in binary. Example: bool bval = true; stored as 1 int ival = 1 stored as 00000000000000000000000000000001
THE BOOL TYPE For example, the bool type has only two possible values: 0 and 1. Since they are already binary, they are stored as a 0 or a 1. C++ has literal constants to represent each: false = 0 true = 1
THE INT TYPE The int type is stored in memory with 4 bytes of memory One byte = 8 bits A bit is a binary digit -- i.e. 0 or 1 So an integer is made up of 32 bits We will work with the char type for examples, since they only have 8 bits.
BINARY NUMBERS A positive number is represented as a binary number (or base-2 number). You can convert binary numbers to base-10, or vice versa. Example 1: Change 1234 10 to a binary number. Example 2: Change 10011001 2 to a base 10 number
POSITIVE AND NEGATIVE NUMBERS An int can be a positive or negative number, so there must be a method to represent both. The method that is used is called Two s Complement. Converting a positive base-10 number to two s complement is just converting it to binary. To convert a negative base-10 number to two s complement: 1. you convert the positive number 2. you flip all the bits (0s to 1s, 1s to 0s) 3. you add 1
EXAMPLE 3 Convert -5342 10 to a binary number stored in 16 bits.
TWO S COMPLEMENT TO BASE 10 If your number starts with a 0, it is positive, and the conversion is standard. If the number starts with a 1, it is negative: flip all bits add 1 Example 4: Convert 10101010 2 to base-10.
RANGE OF VALUES The range of values that an integer can represent depends on the number of bits it is using. bool: 1 bit char: 8 bits short: 16 bits long or int: 32 bits long long: 64 bits double: 64 bits
RANGE OF VALUES Since each bit can take 2 different values, and integer type can store 2 * 2 * 2 *... * 2 = 2 n different values Half of them are negative Half are positive (including 0)
EXAMPLE 5 What is the range of values that you can represent with a bool short int long long
UNSIGNED TYPES If you put the word unsigned before a type, then the type will only represent positive values. This will give you twice as many positive values, and could be useful for something like a counter. Ex: unsigned int counter = 0;
EXAMPLE 6 What is the range of integers that can be stored in the following types? unsigned char; 0 to 256 unsigned int; 0 to about 4 billion
INTEGER OVERFLOW What happens if you end up with a value that doesn t fit in the type? Example 7: char value = 120; char value2 = value + value;
INTEGER OVERFLOW: DEFINITION (WIKIPEDIA) an integer overflow occurs when an arithmetic operation attempts to create a numeric value that is larger than can be represented within the available storage space
SOME USEFUL CONSTANTS Some useful constants are found in <climits> and can be referred to in your programs: INT_MAX INT_MIN CHAR_MAX CHAR_MIN SHRT_MAX SHRT_MIN
CONSIDER THE FOLLOWING CODE #include <iostream> #include <climits> using namespace std; What is this code doing? Printing powers of 2 When will it stop? never int main() { int val = 1; cout << val << endl; while ( val * 2 <= INT_MAX ) { val *= 2; cout << val << endl; cin.get(); this makes the program pause and wait for the user to enter something } }
CORRECT THE EXIT CONDITIONS OF THE LOOP #include <iostream> #include <climits> using namespace std; int main() { int val = 1; cout << val << endl; while ( val <= INT_MAX / 2) { val *= 2; cout << val << endl; cin.get(); } }
EXAMPLE 8: WHEN ARE THESE CONDITIONS TRUE? HOW COULD THEY BE FIXED? int val; function will wrap around just work as a math equation: if ( val + 8 > INT_MAX )... (val > INT_MAX 8) while( 2*val 3 <= INT_MAX )... (val <= INT_MAX/2 + 3/2 ) if ( val * val > INT_MAX )... (val > sqrt(int_max)) while ( val * -5 >= INT_MIN )... (val > INT_MIN/-5)
MORE USEFUL INTEGER TIDBITS A useful manipulator for booleans is boolalpha bool b = true; cout << b << endl; cout << boolalpha << b << endl; cout << noboolalpha << b << endl; Remember to #include <iomanip> Output: 1 true 1
ESCAPE SEQUENCES (USING THE \ CHARACTER) A few chars that you might want to use: \n newline (like endl, but will work faster) \t tab (in general, better to use setw) \ quotation marks cout << I am bored the string ends before b here cout << I am \ bored\ better \ single quotes
SOME CODE TO PRINT THE LETTERS OF THE ALPHABET for ( char letter = A ; letter < Z ; letter++ ) { cout << letter << endl; } Since char is an integer type, you can use integer operations like +
HOW CHARS ARE ENCODED The char type uses the ASCII encoding. It is 8 bits, so there are a total of 256 different characters that it can represent. This is good enough for the English alphabet, but for internationalization, they created Unicode (16 bits) In C++ you would use wchar for this.
CAN ONLY USE WITH INTEGER TYPES: % double a = 5; cout << a % 2 << endl; compiler error
CAN ONLY USE WITH INTEGER TYPES: SWITCH STATEMENTS int card_val; cin >> card_val; if ( card_val == 1 ) { cout << A << endl; } else if ( card_val == 11 card_val == 12 card_val == 13 ) { cout << Face Card << endl; } else { cout << card_val << endl; } The next slide will have the equivalent switch statement
EXAMPLE: THE SWITCH STATEMENT int card_val; cin >> card_val; switch( card_val ) { } case 1: // executed for card_val == 1 cout << A << endl; break; case 11: // keep going until the break is reached case 12: // keep going until the break is reached case 13: // keep going until the break is reached cout << Face Card << endl; // printed for 11, 12, 13 break; default: // executed for any other card cout << card_val << endl; break;
A COMMON USE char user_input; cout << Enter Y or y to... << endl; cin >> user_input; switch ( user_input ) { case Y : case y : // do something here break; case N : case n : // do something else break; default: // do default action you can just treat it like n // or make them reenter } We should always include a default action