// // Character types // #include <iostream> #include <iomanip> using namespace std; int main () { char letter; int x; letter='x'; cout << "The size of the char is " << sizeof(char) << endl; for(x=0;x<5;x++) { cout << letter << "\t" << int(letter) << "\t" i << hex << int(letter) << endl; cout << dec; letter++; } system("pause"); return 0; }
The size of the char is 1 x 120 78 y 121 79 z 122 7a { 123 7b | 124 7c Press any key to continue . . .
// // Boolean types // #include <iostream> #include <iomanip> using namespace std; int main () { char letter; bool fv,tv; tv=true; fv=false; cout << "The size of a bool is " << sizeof(bool) << endl; cout << "true\t" << tv << "\t" << int(tv) << "\t" << hex << int(tv) << dec << endl; cout << "false\t" << fv << "\t" << int(fv) << "\t" i << hex << int(fv) << dec << endl; system("pause"); return 0; }
The size of a bool is 1 true 1 1 1 false 0 0 0 Press any key to continue . . .
0 + 0 = 0 0 + 1 = 1 1 + 0 = 1 1 + 1 = 10 for ease 1 + 1 + 1 = 11 to add 7 + 4 4 0000 0000 0000 0000 0000 0000 0000 0100 7 0000 0000 0000 0000 0000 0000 0000 0111 ----------------------------------------------- 0000 0000 0000 0000 0000 0000 0000 1011 to subtract 7 - 6 First form the complement of 6 6 = 0000 0000 0000 0000 0000 0000 0000 0110 1111 1111 1111 1111 1111 1111 1111 1001 invert bits 0000 0000 0000 0000 0000 0000 0000 0001 add one ------------------------------------------- 1111 1111 1111 1111 1111 1111 1111 1010 -6 0000 0000 0000 0000 0000 0000 0000 0111 add 7 ------------------------------------------ 10000 0000 0000 0000 0000 0000 0000 0001 1 ignore the final carry as it is overflow.
Bits Low High 16 -32,768 32,767 32 -2,147,483,648 2,147,483,647
int k,x; k=0x7ffffffa; for(x=0;x<10;x++) { cout << k << "\t"; printbinary(k); k++; }
2147483642 0111 1111 1111 1111 1111 1111 1111 1010 2147483643 0111 1111 1111 1111 1111 1111 1111 1011 2147483644 0111 1111 1111 1111 1111 1111 1111 1100 2147483645 0111 1111 1111 1111 1111 1111 1111 1101 2147483646 0111 1111 1111 1111 1111 1111 1111 1110 2147483647 0111 1111 1111 1111 1111 1111 1111 1111 -2147483648 1000 0000 0000 0000 0000 0000 0000 0000 -2147483647 1000 0000 0000 0000 0000 0000 0000 0001 -2147483646 1000 0000 0000 0000 0000 0000 0000 0010 -2147483645 1000 0000 0000 0000 0000 0000 0000 0011
unsigned int k; k=0x7ffffffa; for(x=0;x<10;x++) { cout << k << "\t"; printbinary(k); k++; }
2147483642 0111 1111 1111 1111 1111 1111 1111 1010 2147483643 0111 1111 1111 1111 1111 1111 1111 1011 2147483644 0111 1111 1111 1111 1111 1111 1111 1100 2147483645 0111 1111 1111 1111 1111 1111 1111 1101 2147483646 0111 1111 1111 1111 1111 1111 1111 1110 2147483647 0111 1111 1111 1111 1111 1111 1111 1111 2147483648 1000 0000 0000 0000 0000 0000 0000 0000 2147483649 1000 0000 0000 0000 0000 0000 0000 0001 2147483650 1000 0000 0000 0000 0000 0000 0000 0010 2147483651 1000 0000 0000 0000 0000 0000 0000 0011
17 / 2 = 8 r 1 8 / 2 = 4 r 0 4 / 2 = 2 r 0 2 / 2 = 1 r 0 1 / 2 = 0 r 1 stop read backwards 1710 = 100012
.125 x 2 = 0.25 0 .25 x 2 = 0.50 0 .5 x 2 = 1.0 1 0 stop when zero or you run out of bits .12510 = .0012
0111 1111 (127 in binary) 0000 0100 ---------- 1000 0011
1 = 0 01111111 00000000000000000000000 2 = 0 10000000 00000000000000000000000 4 = 0 10000001 00000000000000000000000 100 = 0 10000101 10010000000000000000000 0.0001 = 0 01110001 10100011011011100010111 1.9 = 0 01111111 11100110011001100110011 -1.9 = 1 01111111 11100110011001100110011
Vendor | Exponent bits | Low | High | Mantissa bits | Decimal Digits | |
---|---|---|---|---|---|---|
IEEE | 11 | -308 | 308 | 52 | 16 | |
Cyber | 11 | -293 | 323 | 48 | 15 | |
IBM 3081 | 7 | -79 | 75 | 56 | 16 | |
VAX D | 7 | -39 | 38 | 56 | 17 | |
VAX G | 11 | -309 | 307 | 52 | 16 | |
Cray | 15 | -2466 | 2465 | 48 | 14 |
#include <iostream.h> #include <values.h> void main() { float maxf= MAXFLOAT; cout << maxf <<end; maxf=maxf+MAXFLOAT; cout << maxf <<endl; } 3.40282e+38 infinity
#include <values.h> #include <iostream.h> void main() { float minf= MINFLOAT; cout << minf << endl; minf=minf/1000000; cout << minf << endl; } 1.4013e-45 0
#include <iostream.h> main() { float value,sum; int count; sum=0.0; value=1e-3; for(count=0;count<1000;count++) sum+=value; cout << sum <<endl; sum=0.0; value=1e-4; for(count=0;count<10000;count++) sum+=value; cout << sum <<endl; sum=0.0; value=1e-5; for(count=0;count<100000;count++) sum+=value; cout << sum <<endl; }When this program is run on a 32 and 64 bit words the results are:
Results 32-bit 64-bit 0.999991 1.000000 1.000054 1.000000 1.000990 1.000000
#include <iostream.h> main() { double eps; eps=1.0; while(1.0+eps > 1.0) eps/=2; eps*=2; cout << "epsilon = " << eps << endl; }When this was compiled and run a 32 bit architecture the result was:
int k=5; float x,y=20.0; x=y/k;
float x; int a=7, b=3; x=a/b;What is the result?
float x; int a=7, b=3; x=static_cast<float>a/b;Now what is the result?
#include <iostream.h> void main() { cout << "char " << sizeof(char) << endl; cout << "short " << sizeof(short) << endl; cout << "int " << sizeof(int) << endl; cout << "long " << sizeof(long) << endl; cout << "float " << sizeof(float) << endl; cout << "double " << sizeof(double) << endl; }I got the following results from running on different architectures:
Size of Variables | ||||||
---|---|---|---|---|---|---|
Type | Vax | Sun | Convex | Cray | Apollo | MS VC++ |
char | 1 | 1 | 1 | 1 | 1 | 1 |
short | 2 | 2 | 2 | 8 | 2 | 2 |
int | 4 | 4 | 4 | 8 | 4 | 4 |
long | 4 | 4 | 4 | 8 | 4 | 4 |
float | 4 | 4 | 4 | 8 | 4 | 4 |
double | 8 | 8 | 8 | 8 | 8 | 8 |
char 1 bool 1 short 2 int 4 long 4 float 4 double 8 Press any key to continue . . .
int main() { int a,b; float c; char d; }
int main() { int a=1; float b=1.2e3; char c='a'; }Local variables are initialized every time the block of code is executed.
#include <iostream> using namespace std; void f(); // function prototype int main() { f(); // call function f f(); // call function f system("pause"); return 0; } void f() // function f { int localX=1; // declare and set local varaible cout << "In Function f " << endl; cout << "localX is " << localX << endl; localX=2; return; }
In Function f localX is 1 In Function f localX is 1 Press any key to continue . . .
#include <limits> #include <float.h> #include <iostream> using namespace std; int main() { int max=INT_MAX; cout << "The maximum value for an integer is : " << max << endl; cout << "Add +1 to go over the max - overflow" << endl; max++; cout << "Now the value is : " << max << endl; cout << endl; float maxf= FLT_MAX; cout << "The Maximum Float Value is : " << maxf <<endl; cout << "Times 1 million to force overflow" << endl; maxf*=1.0e6; cout << "After overflow : " << maxf <<endl; cout << endl; float minf=FLT_MIN; cout << "The Minimum Float Value is " << minf << endl; cout << "Divide by 1 trillion to force underflow " << endl; minf/=1.0e12; cout << "After underflow : " << minf << endl; system("pause"); return 0; }
The maximum value for an integer is : 2147483647 Add +1 to go over the max - overflow Now the value is : -2147483648 The Maximum Float Value is : 3.40282e+038 Times 1 million to force overflow After overflow : 1.#INF The Minimum Float Value is 1.17549e-038 Divide by 1 trillion to force underflow After underflow : 0 Press any key to continue . . .