Why is the value range of byte [-128,127]?

We all know that byte, short, int and long are the eight basic data types in Java. However, we have some doubts about their value range at the beginning of learning. For example: Why is the byte range [-128,127] instead of [-127,127]?

To make sense of this, you need to understand a few concepts first (8-bit binary as an example) :

Number of machines

We all know that the computer only know 0 and 1, only know binary, machine number is the binary representation of the number in the computer, machine number is signed, usually the symbol bit in the highest bit, 0 represents a positive number (+), 1 represents a negative number (-).

Since a sign occupies one bit, the formal value of the number is not equal to the real value, and the value of the machine number with the sign bit is called the truth value of the machine number. For example, the binary truth number -0011011 has a machine number of 10011011.


The original code

True form is a binary fixed-point representation of numbers in a computer. Source code = sign bit + absolute value of truth value.

Such as:

+ 1 = [00000001] of the original

1 = [10000001] of the original

0 can be represented in two ways:

+ 0 = [00000000] of the original

0 = [10000000] of the original

  • Advantages: simple and intuitive, easy to understand by the human brain.
  • disadvantages: Cannot directly join the operation with the sign bit. It may cause errors. For example, in mathematics, 1+(-1)=0, whereas in 8-bit binary [00000001]The original+ [10000001]The original= [10000010]The original, which is -2 in decimal notation. Clearly something has gone wrong.

Radix-minus-one complement

The inverse of a positive number is the same as the original code.

The inverse of a negative number is based on its original code, the sign bits remain unchanged, and the rest are reversed by bits, that is, 0 becomes 1,1 becomes 0.

Such as:

+1 = [00000001] Original = [00000001] Negative

-1 = [10000001] Original = [11111110] negative

There are still two ways to represent 0:

+0 = [00000000] Original = [00000000] Reverse

-0 = [10000000] Original = [11111111] Negative


complement

The complement of a positive number is the same as its source.

The complement of a negative number is added to its inverse by 1.

Such as:

+1 = [00000001] Original = [00000001] Inverse = [00000001] Complement

-1 = [10000001] Original = [11111110] inverse = [11111111] complement

There’s only one way to write 0:

+0 = [00000000] Original = [00000000] Inverse = [00000000] Complement

-0 = [10000000] Original = [11111111] Inverse = [00000000] Complement

To reassure

Byte is in the range of [-128,127] instead of [-127,127]. Computers use binary to represent numbers. The maximum is [01111111] for 127. There is no doubt about this. Why [10000000] stands for -128? Did I learn a fake binary??

In fact, it is not a binary problem, but the computer uses complement code to represent numbers, rather than the original code, as for why the use of complement does not use the original code, here is not detailed, interested can go to understand.

(-1) + (-127) = [10000001] Original + [11111111] Original = [11111110] Inverse + [10000000] Inverse = [11111111] Complement + [10000001] Complement = [10000000] Complement

That’s the answer to the question.

[10000000] complement represents -128, which is obtained through the operation of complement, but it does not have the representation of original code and inverse code, because it actually occupies the position of -0, and it is wrong to use [10000000] to complement the original code.

Wrong example:

[10000000] ‘complement’ = [01111111] ‘inverse’ = [00000000] ‘original’

Extension (byte, short, int, long)

The value range depends on the number of bytes used to store data of these types.

  • 1 byte = 8 bits

Byte: stores one byte of data. The value ranges from -128(-2^7) to 127(2^ 7-1) short: stores two bytes. The value ranges from -32,768(-2^15) to 32,767(2^ 15-1) int: Stores two bytes. The value ranges from -2,147,483,648(-2^31) to -2, 147,483,647(2^ 31-1) long: To store data of eight bytes, value range is 9223372036854775807-9223372036854775808 (63) – 2 ^ ~ (2 ^ 63-1)