I want to know why int, double etc have 1 more negative value than positive value.
-
8[Probably a good read.](http://en.wikipedia.org/wiki/Two's_complement) – chris Jan 12 '13 at 07:23
-
2Because the simplest and most mathematically convenient representation of signed integers works that way. – Louis Wasserman Jan 12 '13 at 07:24
-
In any representation where you're going to make all bit patterns meaningful, you always have `2^N` representations to assign meaning to. You either end up with two representations that actually mean the same value or with one extra positive or negative number (or wierdness like `+/- 0` which are sometimes equal, sometimes not) – Damien_The_Unbeliever Jan 12 '13 at 07:26
-
7`double` generally does not have 1 more negative value than positive value. It usually has the exact same number of positive values as negative values. – Benjamin Lindley Jan 12 '13 at 07:29
-
Actually there are also representations that are symmetric (ones complement). They use a separate bit pattern for -0. – Henry Jan 12 '13 at 07:29
-
This is suspiciously similar, and 2 days old: http://programmers.stackexchange.com/questions/182126/why-is-the-minimum-value-of-ints-doubles-etc-1-farther-from-zero-than-the-posi – Corbin Jan 12 '13 at 07:31
-
1This depends on language _and_ implementation. – stefan Jan 12 '13 at 07:34
-
2@stefan: For Java, it doesn't depend on the implementation. And for C and C++, can you name an implementation that didn't use two's complement? I'd be surprised if one existed, other than someone doing it as a hobby in their basement, not to be used by anyone in production. – Benjamin Lindley Jan 12 '13 at 07:48
-
1@BenjaminLindley According to http://en.wikipedia.org/wiki/Ones%27_complement it was used by CDC 6000 series (designed by Seymoour Cray) and Univac 1100 (ISA still supported, according to the article). I've heard of it being used in Burroughs machines. Also, some DSPs implement properties of floating-point math without actually having FP. – Potatoswatter Jan 12 '13 at 08:01
-
Voted to reopen. How is this too localized? Close as a duplicate if anything – Potatoswatter Jan 12 '13 at 08:03
-
@Potatoswatter: But the question is about Java, C and C++. I'm quite certain a Java has never been implemented on either of those systems, but even if it was, it would have to use two's complement to be Java. And I also doubt C or C++ was ever implemented for either of those systems, other than possibly as a hobby project that was never used in production (and I even doubt that). – Benjamin Lindley Jan 12 '13 at 08:10
-
In 2's complement (3-bit integers) the _weight_ of the MSB is -4, as e.g. -1 = '111' = -4 + 2 + 1. and '001' is 0 + 0 + 1. So it makes really sense that '100' _is_ -4 instead of 4 with the obvious consequence. – Aki Suihkonen Jan 12 '13 at 09:01
-
1`double` has the same number of negative as positive values because it has a sign bit. This means it has +0.0 and -0.0. Integer value are either negative or non-negative (same number of each) 0 is one of the non-negative leaving one less positive value. – Peter Lawrey Jan 12 '13 at 10:50
-
@Potatoswatter It *is* an exact duplicate, but not on StackOverflow (http://programmers.stackexchange.com/questions/182126/why-is-the-minimum-value-of-ints-doubles-etc-1-farther-from-zero-than-the-posi). The top answer there is even "0 needs a spot" same as on this question. And to top it off, the Programmers question was asked 2 days before this one. Oh, and googling the title of this question brings that one up. Ridiculous. Can't close a duplicate across the SE network so "too localized" seems like the next best choice. – Corbin Jan 13 '13 at 00:47
-
Your question is incorrect. It's true for 2's complement integer types but not for double – phuclv Mar 01 '15 at 02:32
3 Answers
In a nutshell: 0 has to fit somewhere, it is in the positives, which makes them have one less than the negatives.
Example: 5 slots for negatives and 5 for positives, negatives get -1 to -5, positives get 0 to 4
- don't forget to read @chris link :)
As @WhozCraig pointed out, this is only valid for architectures that use two's complement representation of signed binary numbers.

- 410
- 5
- 10
-
5+1 this is a well-put answer for something that all-too-often comp engineers forget about two's-comp. (at least for int-twos-comps). The simple phrase "`0` has to fit somewhere." is catch-phrase memorable. – WhozCraig Jan 12 '13 at 07:37
-
-
@DavidRF Yeah, I probably would have clarified in his answer that this is with respect to numbers that store signed-ness using a two-complement format, which is really what this answer is all about, but that single phrase was worth the upvote for me. If the author doesn't edit it, I just might to clarify that point. – WhozCraig Jan 12 '13 at 07:58
-
1@Arnaud caught me mid-edit, but you summed it up. Good for me. Still like that phrase. – WhozCraig Jan 12 '13 at 08:15
-
And having `0` on the positive side is good, since it allows positive signed and unsigned to have the same representation. – zch Jan 12 '13 at 14:26
Assume that you have an integer datatype that uses 4 bits. You can represent 16 possible signed integers with them. Positive integer values are assigned to the first half of the range:
0000b = 0
0001b = 1
0010b = 2
0011b = 3
0100b = 4
0101b = 5
0110b = 6
0111b = 7
For the second half, there are two choices:
- Map the 8 positions to integers
-1
to-7
and the special value-0
. This is used by one's complement and sign-and-magnitude representation of numbers. - Map the 8 positions to integers
-1
to-8
. This is used by two's complement representation which most programmers are familiar with (and the one you're talking about).
The negative numbers are mapped like this:
1000b = -8
1001b = -7
1010b = -6
1011b = -5
1100b = -4
1101b = -3
1110b = -2
1111b = -1
While this might not make sense, this mapping makes it easy to perform arithmetic operations.
This does not apply to floats; they are represented differently. Most floating point representations have equal range on either side of +0/-0.

- 262,204
- 82
- 430
- 521
For C, it's not guaranteed that you'll be running on a machine with two's complement arithmetic hardware (one more negative than positive). In reality, it'll probably be that way though.

- 15,885
- 2
- 53
- 56
-
1It's effectively guaranteed. I'm not aware of any significant architectures designed in the last 30+ years which don't use two's complement. – Jan 12 '13 at 07:41
-
2Of course there are also non-significant architectures, not designed in the last 30 years, that still have C compilers. See [Exotic architecture the standard committee cares about](http://stackoverflow.com/questions/6971886/exotic-architectures-the-standard-committee-cares-about) – Bo Persson Jan 12 '13 at 11:35