T
T
Tarasov Konstantin2014-05-24 20:14:10
Java
Tarasov Konstantin, 2014-05-24 20:14:10

Why is the bounds of primitive integer types in java in - 1 more than in +?

The subject itself, for example a byte, can contain a number from -128 to 127, with other types the same. What is the reason for this?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
L
LPzhelud, 2014-05-24
@saroff

Because zero is also considered positive.
Therefore, the numbers in the positive range must fit one more.

S
svd71, 2014-05-24
@svd71

this is due to the fact that there is another universal meaning, like a sign. It has one bit. the most left. that is, if this left bit is separated by a binary AND operation, then you can immediately tell whether the number is positive or negative.

M
mrstrictly, 2014-05-24
@mrstrictly

Since the internal representation of data types uses a binary number system, the cardinality of the set of numbers that can be represented by integer types will always be even. We agree that the set of integers of cardinality N represents N/2 positive and N/2 negative numbers, for example. Where to put zero? We "take away" it from negative numbers ...
This is, in a nutshell.

P
Puma Thailand, 2014-05-25
@opium

Actually, a byte is 2 to the eighth power, or 256 different values ​​\u200b\u200bin
the range -128 +127, just their 256
in general, no one bothers changing this range to -125 +130 , for example,
well, there are unsigned ranges 0 255 or 1 256

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question