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
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
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.
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?
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.
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.
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.
@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 ?
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
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.