2

In most programming languages where integral datatypes have a finite range,there is always one more negative number than positive numbers.

For instance,in C,a byte is -128~127, and an int is between -2^31 and 2^31-1 inclusively. Is there a reason why a byte is not -127~128,since positives occur more frequently in intuitive sense?

Fermat's Little Student
  • 5,549
  • 7
  • 49
  • 70
  • Note that there are as many *non-negative* integers as there are negative ones. Don't forget 0! –  Nov 04 '12 at 03:41

4 Answers4

3

the largest positive is 0111 1111 = 127

128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
 0  | 1  |  1 |  1 | 1 | 1 | 1 | 1 |

The largest Negative byte is 1000 0000 = -128

-128| 64 | 32 | 16 | 8 | 4 | 2 | 1 |
 1  | 0  |  0 |  0 | 0 | 0 | 0 | 0 |

In binary the MSB(Most Significant Bit - the front one) is reserved to signify a negative number. The concept is called Twos' Complement and is used by most computers as a way of representing integers in binary(base 2) notation.

To get more info look into binary calculations

Community
  • 1
  • 1
1321941
  • 2,139
  • 5
  • 26
  • 49
  • 1
    @delnam -- I don't remember, does the C standard require two's compliment, or is that just the most common? Not that anyone actually *uses* one's compliment anymore. – Jeremy J Starcher Nov 04 '12 at 03:47
  • @JeremyJStarcher I just came across [Are there any non-twos-complement implementations of C?](http://stackoverflow.com/q/12276957/395760) which tells us C doesn't require 2*. Yeah, we're not strictly talking standard C here, but practically this is all 99% of us will ever meet. No need to get overly pedantic :) –  Nov 04 '12 at 03:52
0

It's because of 2's complement notation. The sign bit is 0 for positive, 1 for negative. So, using 4 bits as a simpler example:

Positive: 0 is 0000, 1 is 0001, etc. up to 0111 as 7.

Negative: -1 is 1111, -2 is 1110, etc, down to 1000 as -8.

AlwaysBTryin
  • 1,924
  • 12
  • 7
0

(I know this doesn't answer your question, but it does address an untrue assumption in your question and it is too long to leave as a comment.)

Actually, the C standard does not define the size of a byte.

The only thing that is assured is that char will be able to hold one character.

In the past, bytes have ranged between 5 and 9 bits, depending upon the CPU.

It is true that most of that wildness has settled down and most systems in place do use an 8-bit byte.

// What the C standard says must be true:
sizeof char <= sizeof int <= sizeof long

This is why many pre-c99 (and c99) systems included the extremely useful typedefs of:

int8
uint8
int16
uint16
int32
uint32
Jeremy J Starcher
  • 23,369
  • 6
  • 54
  • 74
  • If we're going to be pedantic: (1) The C standard does, however, define *minimum* ranges for the types you list. (2) the fixed-size integer types *have* been standardized, just later, in C99. (3) There have also been reports of (non-ancient) embedded system where a "byte" is several octets large. –  Nov 04 '12 at 03:48
  • @delnan -- Thanks for the heads up there. By the time C99 became popular, I was already out of most C development. As far as being pedantic, guilty as charged. In my defense, C is a pedantic language. C developers are known to end up working in low-level areas and thus should be much more careful of the 'one-size-fits-all' mindset. – Jeremy J Starcher Nov 04 '12 at 03:54
0

In most programming languages where integral datatypes have finite a range,there is always one more negative number than positive numbers.

This is because 2's complement is almost always used.

The reason two's complement is so popular basically comes down to hardware reasons. In particular:

a - b = a + (~b + 1)

Example (4 bit words):

0110 - 0101 = 0110 + 1010 + 1 = 0110 + 1011 = 0001 (note that the addition steps are essentially unsigned addition -- there's no special handling of the sign bit in those steps)

Basically, in hardware-land, you can change a - b into an addition with a + ~b + 1 with the initial carry set to 1. This can be quite a useful trick. There's no special care required for subtraction, which means it doesn't require its own circuit.

Corbin
  • 33,060
  • 6
  • 68
  • 78