Back to Weekly Schedule
A C++ Program that overflows
// mulby2.cpp
// D. Thiebaut
// demonstrates how a compiled language deals with an overflow.
// check mulby2.py to see how an interpreted language will do.
//
#include <iostream.h>
main() {
unsigned int x, lastx;
cout << endl << endl << endl << endl;
x = 1;
cout << "x = " << x << endl;
while ( 1 ) {
lastx = x;
x = x * 2;
if ( x <= lastx ) {
cout << "last x = " << lastx << endl;
cout << "x = " << x << endl;
break;
}
}
cout << endl << endl << endl << endl;
}