|
-
Nov 8th, 2002, 03:52 PM
#1
Thread Starter
Hyperactive Member
32768+1 = -32768 ???
I am just starting with C, and in most languages you get overflow or some error about integer overflow or something....
My question is, why does C go to -32768, and then counts up to 0, then to +32768 again, and it becomes a constant loop.
I thought it would have something to do with binary, and that:
11111
when added, if you only have x spots, it becomes
00000
I may be wrong, but any explanation is appreciated!
-
Nov 8th, 2002, 04:18 PM
#2
Fanatic Member
Test to add unsigned in front of the declaration of the integer, that will make the integer 0-based.
-
Nov 8th, 2002, 05:28 PM
#3
Frenzied Member
Code:
int i; - this means it interprets the most significant bit as +/-
unsigned int j; this means it does not interpret that bit as a sign
Hence you can reach a higher absolute value with j than with i;
-
Nov 8th, 2002, 05:30 PM
#4
Thread Starter
Hyperactive Member
Yes, I understand that unsigned can go up to 65xxx
but my question is, WHY does it go go 32767, 32768, -32768, -32767
thanks.
-
Nov 8th, 2002, 05:43 PM
#5
Fanatic Member
-
Nov 8th, 2002, 08:34 PM
#6
You've already answered it. The solution lies in the bits. It simply wraps around.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 8th, 2002, 11:58 PM
#7
Thread Starter
Hyperactive Member
but if 11111 become 00000, why does it start again at -, and not at 0 ?
-
Nov 9th, 2002, 06:55 AM
#8
Because of the format used to represent negative numbers. It's called 1's complement.
11111111 is -1, not -32,some as one would think.
To get the negative of any number you invert every bit, then add 1.
00100011 (35) -> 11011100+1=11011101 (-35)
00000001 (1) -> 11111110+1=11111111 (-1)
00000000 (0) -> 11111111+1=00000000 (0)
Whenever the CPU needs a substraction, it converts the number to be substracted to negative and adds it:
5-1
00000101 - 00000001 = 00000101 + 11111111 = 00000100 (with the highest 1 being cut off) = 4
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 9th, 2002, 06:58 AM
#9
The highest positive number is 01111111 (127), the highest negative is 10000000 (-128). If you add 1 to 127 you get -128
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 9th, 2002, 02:15 PM
#10
Thread Starter
Hyperactive Member
-
Nov 10th, 2002, 02:41 PM
#11
Fanatic Member
The highest number for signed 16 bit integers is 32767 (2^15-1) instead of 32768. This is because of the existance of zero.
-32768 is still the lowest value.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|