-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.

12
  • The minimum value is not 0... Commented Jun 4, 2018 at 11:21
  • 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! Commented Jun 4, 2018 at 11:24
  • 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 Commented Jun 4, 2018 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) Commented Jun 4, 2018 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.) Commented Jun 4, 2018 at 11:32

2 Answers 2

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 
Sign up to request clarification or add additional context in comments.

Comments

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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.