It's fairly simple, but lets make it simpler and look at it as if it was a three bit integer, with the top bit (bit 2) being the sign bit:
BIN DEC
000 = 0
001 = 1
010 = 2
011 = 3
100 = -4
101 = -3
110 = -2
111 = -1
So when you reduce zero by one, you get all ones, and this is minus one in decimal.
Flipping the sign bit does not reverse the sign.
"Sorry, I just tested your example on two integer types: byte and unsigned byte. In both cases:
100 = 4
101 = 5
110 = 6
111 = 7
Besides, how can possibly it make any sense: a jump from 3 to -4. Why?! - programmer095 13 hrs ago"
Read what I said again:
lets make it simpler and look at it as if it was a three bit integer
An
int
in C# is a 32 bit integer, so it follows rules for binary right up to 2 Gig!
For 32 bit numbers:
Dec Bin
0 00000000000000000000000000000000
1 00000000000000000000000000000001
2 00000000000000000000000000000010
3 00000000000000000000000000000011
4 00000000000000000000000000000100
...
-1 11111111111111111111111111111111
-2 11111111111111111111111111111110
-3 11111111111111111111111111111101
...
Which is a little difficult to read: hence the imaginary three bit integer!
A
byte
is 7 bits plus sign (or 8 for unsigned) so the range of +127 to -128 is a little large to expand as well...
[edit]Added byte info - it's early, and I'm still on my first coffee...-OriginalGriff[/edit]