-1

If Java's long primitive data type is a 64-bit signed integer, then why do the Java docs state that the static long maximum value is 2^63-1? Is there a larger reason that affects other primitive data types similarly in Java?

I'm learning Java and am just genuinely curious about this disparity. Thank you.

Alejandro
  • 623
  • 1
  • 9
  • 22
  • The minimum value is not 0... – assylias Jun 04 '18 at 11:21
  • A signed value needs to be able to distuinguish between `x` and `-x`. That's why the maximum value is not the size. – Ben Jun 04 '18 at 11:22
  • What do you mean @assylias? – Alejandro Jun 04 '18 at 11:23
  • The mechanism is called "Two's complement". – Ben Jun 04 '18 at 11:24
  • 1
    What he means is that there are `long` values less than zero. In fact, there are **more** `long` values less than zero than greater than zero! – Stephen C Jun 04 '18 at 11:24
  • https://en.wikipedia.org/wiki/Two%27s_complement – Stephen C Jun 04 '18 at 11:25
  • 2
    in any programming language, the Most Significant Bit will be the sign bit, which denotes the sign of the number. So in your question, for a 64-bit signed integer, leaving the MSB, the remaining 63 bits are used to denote the value of the integer. Which is why 2^63-1 – Ashwin K Kumar Jun 04 '18 at 11:26
  • 4
    @AshwinKKumar that is wrong. Java uses the "Two`s Complement" and does not just use the most significant bit for the sign (the fact that the msb is 1 for negative numbers is true but the remaining bits are not the same as for the positive number) – Ben Jun 04 '18 at 11:28
  • 1
    *"in any programming language, the Most Significant Bit will be the sign bit"* - Incorrect. 1) In a programming language with unsigned integers, the top bit is not the sign bit. 2) You are assuming the programming language uses the hardware-provided native (signed) integer representation. In some languages, that is not always true ... or never true. (Example of "never true" - the CLU programming language, which "stole" a bit for use by the GC to distinguish scalars from pointers.) – Stephen C Jun 04 '18 at 11:32
  • Read this https://stackoverflow.com/questions/1049722/what-is-2s-complement – jschnasse Jun 04 '18 at 11:35
  • Ahh. My bad, I went through the Java Documentation to clear things up. Shouldn't have assumed things. – Ashwin K Kumar Jun 04 '18 at 11:39
  • I don't get the question - what disparity? *64-bit **signed** integer* ranges from `-2^63` to `+2^63-1` (-1 to count for `0`) – user85421 Jun 04 '18 at 11:41

2 Answers2

4

Java's long type is indeed a 64-bit integer, but it is also signed.

With 64-bit, you can represent 2^64 different numbers. If you ignore all the negative numbers, then the maximum value would be 2^64-1, and minimum will be 0. 0 plus all 2^64-1 positive numbers is 2^64 numbers in total.

However, if you consider all the negative numbers as well as the positives, about half of the 2^64 different numbers will be negative. Java just chose to represent 2^63 negative numbers, the number 0, and 2^63-1 positive numbers. If you add all these up, you get 2^64 total numbers.

2^63 + 1 + 2^63 - 1 = 2^64
       ^
       |
  this is the number zero
Sweeper
  • 213,210
  • 22
  • 193
  • 313
0

Its just a range formula just like range for octal and hexadecimal numbers 2^n -1 different for signed and unsigned numbers. 2^n and 2^n-1(Range)

Sport
  • 8,570
  • 6
  • 46
  • 65
Talha Shan
  • 11
  • 3