• Minimum/maximum hexidecimal values in C++
    4 replies, posted
Hi all. For a computer science course, I am required to write a program in C++ that stores the maximum integer (in hexidecimal form) into a variable, displays it, then adds any positive integer to it in order to show how C++ handles overloading. This is my code so far: [code] #include <iostream> #include <cstdlib> using namespace std; unsigned int maxInt = 0xFFFFFFFF; int numberToAdd = 0xF; int main() { cout << maxInt << endl; cout << numberToAdd << endl; cout << "\n" << numberToAdd + maxInt << endl << endl; system("pause"); return 0; } [/code] The output is the following: [code] 4294967295 15 14 [/code] As you can see, the number resets itself to 0 and continues adding when the maximum value is exceeded. This is fine and works; HOWEVER, I have to do the same thing with the largest negative number (take the largest negative number and subtract a number from it to see what happens). Obviously, I had to add "unsigned" in front of the integer variable in order to store 8 hexadecimal values and get the largest possible value. So my question is, how can I use the largest negative hexadecimal value and add a negative sign in front of it? Keep in mind that it has to be stored into a variable. I apologize if this is a dumb question, but this week is the first time we've even learned how to translate to/from hexidecimal notation, and my teacher failed to mention how to use hexidecimal notation in C++ at all (I had to google the fact that you have to add 0x before the characters for the compiler to know). Thanks you guys for your help.
8 hexadecimal digits* Look up std::numeric_limits. Also, the effect is only guaranteed for unsigned integers. The behavior for signed integers is not defined by the C++ standard.
The largest negative number for a 32bit int is -2147483648 which is 0x80000000 in hex. To understand why you'll need to know how negative numbers are stored in binary([url=http://en.wikipedia.org/wiki/Two's_complement]Two's complement[/url]).
[QUOTE=Teap;27546262]Hi all. For a computer science course, I am required to write a program in C++ that stores the maximum integer (in hexidecimal form) into a variable, displays it, then adds any positive integer to it in order to show how C++ handles overloading.[/QUOTE] I think you mean overflowing, not overloading.
Yes I meant overflowing. Whoops.
Sorry, you need to Log In to post a reply to this thread.