Run the following commands in a program and observe the result. Turn in a writte
ID: 3783371 • Letter: R
Question
Run the following commands in a program and observe the result. Turn in a written description of what was output, as well as an explanation of why the values output were produced.
int w = 2147483648;
cout << w-1 << endl;
cout << w << endl;
cout << w+1 << endl;
cout << w+2 << endl;
unsigned u = 4294967296;
cout << u-1 << endl;
cout << u << endl;
cout << u+1 << endl;
cout << u+2 << endl;
Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
//Visual Studio 2008 on a 32-bit architecture int and long both are 4 bytes
int w = 2147483648; //signed int ragge : -2147483647 to 2147483647
cout << "w-1 = "<<w-1 << endl; // display correct value after deducting 1 from w
cout <<"w = " <<w << endl; // when you add one to the maximum value of int, you get the minimum value of int as its implemented as circlular
cout << "w+1 = "<<w+1 << endl; //minimum +1
cout << "w+2 = "<<w+2 << endl; //minimum +2
//unsigned int range is 0 to 4294967295
unsigned u = 4294967296;
cout << "u-1 = "<< u-1 << endl; //in range
cout << "u = "<< u << endl; // min value =0
cout << "u+1 = "<< u+1 << endl; // 0+1 =1
cout << "u+2 = "<< u+2 << endl; // 0+2
return 0;
}
output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.