Expanding a bit on what trem said:
A single 8-bit integer (one byte) can hold 256 distinct values: 0 to 255. Well if you only have to worry about positive numbers you're fine. But what about negatives? Negatives are expressed by using a signed-type integer. Basically you take the left-most bit (the most significant) and make it a zero for positives and a one for negatives. That means you are cutting half the positive numbers you can use out and making them negative. Not exactly half though, because 0 is neither negative nor positive yet still needs to be represented, so you lose one of the positive numbers.
So basically you have 0, 1, 2, 3, [label start]4, all those other numbers we love so much, 125, 126, 127... Let's stop there. 127 is a good number. Strong, confident, like a goat in a tower
. It's the start of the loopback IP address (127.0.0.1). It's a Mersenne prime
, a palindromic binary number, the number of hours some jackass was stuck underneath a boulder, and a lot of other things
. 127 is also about half of 256. So let's do some math. We have 256 total numbers we can represent. One of those is set aside for zero, leaving us with 255. 127 of those are for positive numbers, so that leaves 128 left over. Now let's see...
Aha! Let's take those remaining 128 numbers and make them negative! Fuck da police! Back to counting:
124, 125, 126, 127... *deep breath* Negative one-hundred-fucking-twenty-eight. -127. -126. This is feeling dirty but I like it. -125. -124. Oh yea baby. -123. -122... -3, -2, -1... 0. 1. 2. 3. [goto start].
*ahem* So. That's if you are signed. After 127 comes -128. But if you are unsigned, positive 128 follows positive 127. So why is -1 equal to positive 255 in your program? I don't know. You figure it out.
Obligatory StackOverflow post: https://stackoverflow.com/questions/114 ... nd-not-fro