Newbie needs explanation, int_max

ok so i ran this program for class regarding INT_MAX. when i did it, i found that the INT_MAX for my computer was 2137483647 but when i when added 1 to INT_MAX, it became -2137483648. i'm not sure why this happened. why did the number become negative, and why was it 48 at the end of the number instead of 47. is there a name for this occurrence? please help a noob out! thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <climits>
using namespace std;
int main()
{
int mynumber = INT_MAX;
cout << mynumber << endl;
mynumber++;
cout << mynumber << endl;
cin.get();
return 0;

}


INT_MAX is the maximum number an int can have,
INT_MAX+1 in many cases produces that the values wraps to the minimum number an int can have ( which is negative )
The positive numbers (in your case) go from 0 to 2137483647,
negatives numbers go from -1 to -2137483648.
Since the positives have the 0, they don't have 2137483648
i see so since it can's go passed the max, it just reverts back to the max negative value? , thanks alot...is this called "wrap around"?
You have to understand two's complement and binary numbers to understand this phenomenon.

On your platform, an int is 32 bits. To make the math easier, I'll use a 4-bit int here, but the principle extends to 32 bits and beyond.

In two's complement, the most significant bit is a sign bit. 0 means positive (non-negative) and 1 means negative.

0111 is therefore the largest binary value that can be stored in a 4-bit number, and its equivalent is 7 decimal. Adding
1 to that value yields 1000, which flips the sign bit from 0 to 1, so suddenly the number becomes negative. Now, if you
understand two's complement, this corresponds to the most negative value that can be stored in a 4-bit number, which
is -8.

Here is a table:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Binary            Decimal
0000                  0
0001                  1
0010                  2
0011                  3
0100                  4
0101                  5
0110                  6
0111                  7
1000                -8
1001                -7
1010                -6
1011                -5
1100                -4
1101                -3
1110                -2
1111                -1


(and then adding 1 to 1111 yields 0000, which is zero, and makes mathematical sense).
Last edited on
It's also INT_MAX is a different number on different computers eg. different on a 286 to a 386 and 486, or different on 32bit processors to 64bit processors to be more precise.
@ jsmith that's alot man, i under why it turns negative now. but i just have a question about the bits. you said on my system, and int is 32 bits, does this mean the decimal number 7 is represented as 00000000000000000000000000111 in binary on my computer?
closed account (j3bk4iN6)
I'd like to add if you wanted to write a program that converted two's complement binarys to integer then you would subtract by using if(binaryInteger > 2**bitSize/2) after you converted, then you could subtract binaryInteger - 2**bitSize to get negative integer. since in two's compliment 4 bits has a total of 16 integers, 8 positive and 8 negative. and the 0111 number is 0+4+2+1. all bits are determined by 2**n, where n is bit number so an 8 bit binary is
128,64,32,16,8,4,2,1 which is a total of 256 bits, so the last bit is 2**n/2 = 128. Once you hit 11111111 which is 255 if you add one bit to that go to 00000000, I haven't learned yet what happens to the bits that carry out of range in this instance.


** refers to power of, I can't remember the power funciton in C++ off the top of my head.
Last edited on
To make it easy, remember this rule:
- When a variable goes above its maxium value, it starts off again from the minium value.

Example: A char variable has the max value 128 and min value -127. So if you have this:
char c = 128;
And you do this:
c++;
It wants to make 129 but that is unpossible so you'll get -127. With this:
1
2
c = 128;
c += 10;

you want to add 10 to 128, normally you'll get 138, but you'll get -118 because you'll start of from the minium again.

Yes, this all can be explained through all those complicated things, but it isn't nessesary.
Last edited on
it's actually min -128 and max 127

but yeah
thansk guys but i confused about the bits. i know he used 4 bits as an example there. so to represent decimal number 7 in 4 bits is 0111. but like he said, in my computer an int is 32 bits so does that mean decimal number 7 is represented as 00000000000000000000000000111 ? i'm really curious.
closed account (j3bk4iN6)
32 bits is 2**32 so all the zeros are the bits that are turned off in the series.

To understand twos compliment when you want to get a negative value of the integer you flip the bits then add a bit

0110

1001
0001 (carry the ones)
1010 is negative 6

you'll notice that 10 is out of the range of 8 so its negative, usually a binary beginning with 1 at the end is negative, except when its 1000.
Last edited on
@bilify i understand 2's complement but just the bits is annoying me, so decimal number in 32 bits is 00000000000000000000000000111 ? even in 32 bits 7 can just be represented as 111 instead of 00000000000000000000000000111 ?
closed account (j3bk4iN6)
thats why we work in 4's or 8's so we don't have to write all the zeros, but keep in mind that a 32 bit negative is going to be different than its 4 bit equivalint. You need the zeros to justify the bit size, you could write it as 111, but the place holder 0 helps to determine where 111 is in the series, so lower numbers like 7 are best described in 4bits because the rest are zeros regardles of bit size. you wanna keep bits by powers of 2 starting with 4 to keep it organized. so 4,8,16,32,64
Last edited on
so im guessing that's a yes. thanks.
@jinjin12:

The answer to your question is yes. ints are 32 bits on 32-bit machines/OSes. Even when representing 7, as you
asked.
Although it won't be that way for long, within about 5 years is my guess, just about everyone will be using 64bit operating systems with 64bit designed software.. We already use 64bit Cpu's as home PC's so it's just a matter of time before the software world catches up and starts writing 64bit software.
Topic archived. No new replies allowed.