14

The upper limit for any int data type (excluding tinyint), is always one less than the absolute value of the lower limit.

For example, the upper limit for an int is 2,147,483,647 and ABS(lower limit) = 2,147,483,648.

Is there a reason why there is always one more negative int than positive int?

EDIT: Changed since question isn't directly related to DB's

Mark He
  • 735
  • 7
  • 14

3 Answers3

8

The types you provided are signed integers. Let's see one byte(8-bit) example. With 1 byte you have 2^8 combinations which gives you 256 possible numbers to store.

Now you want to have the same number of positive and negative numbers (each group should have 128).

The point is 0 doesn't have +0 and -0. There is only one 0.

So you end up with range -128..-1..0..1..127.

The same logic works for 16/32/64-bit.

EDIT:

Why the range is -128 to 127?

It depends on how you represent signed integer:

Community
  • 1
  • 1
Lukasz Szozda
  • 162,964
  • 23
  • 234
  • 275
6

This question isn't really related to databases.

As lad2025 points out, there are an even number of values. So, by including 0, there would be one more positive or negative value. The question you are asking seems to be: "Why is there one more negative value than positive value?"

Basically, the reason is the sign-bit. One possible implementation of negative numbers is to use n - 1 bits for the absolute value and then 0 and 1 for the sign bit. The problem with this approach is that it permits +0 and -0. That is not desirable.

To fix this, computer scientists devised the twos-complement representation for signed integers. (Wikipedia explains this in more detail.) Basically, this representation maintains the concept of a sign bit that can be tested. But it changes the representation. If +1 is represented as 001, then -1 is represented as 111. That is, the negative value is the bit-wise complement of the positive value minus one. In fact the negative is always generated by subtracting 1 and using the bit-wise complement.

The issue is then the value 100 (followed by any number of zeros). The sign bit is set, so it is negative. However, when you subtract 1 and invert, it becomes itself again (011 --> 100). There is an argument for calling this "infinity" or "not a number". Instead it is assigned the smallest possible negative number.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • Thanks for the help Gordon, I understood it after reading the Wiki. I think you mean -1 is represented as 111 and the two's complement 0 is all 0's and that's why there is one more negative value than positive. Thanks again! – Mark He Nov 13 '15 at 20:00
  • @MarkHe . . . Thank you very much. I wrote the original from memory, and it's been a while since those computer science courses. I fixed the answer. – Gordon Linoff Nov 14 '15 at 12:41
  • "That is, the negative value is the bit-wise complement of the positive value minus one. ". This is wrong. Negative value in 2s complement is always the bitwise complement of the positive value PLUS one. Its vice versa is also the same, that is, positive value in 2s complement is always the bitwise complement of negative value PLUS one. Your quote is basically stating the opposite to yield the same result. But it might confuse someone learning representation of negative integers using 2's complement. You always perform inverse-then-add-one to convert either ways, -ve to +ve and +ve to -ve. – ZeZNiQ Feb 15 '20 at 15:35
0

Let's say you have a 4byte (32 bit) integer. The range defined by C++ is -231 to 231-1. So we end up with a range -231.....0......231. We can think of this as having 231 non negative integers (note 0 is included) and 231 negative integers.

pepop99
  • 27
  • 4