This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubt in long datatype  RSS feed

 
RajendraKr Kumar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello friend,
I have some doubt in long datatype, As i know any long datatype can store max value 9223372036854775807 which i calculated to be 63 bit long only. But by default long datatype can store 64 bit value and max capacity should be 18446744073709551615. I wanted to store 64 bit value(18446744073709551615) in long datatype but it is not being accepted.

long ts=18446744073709551615L;

It is giving error like "Integer number too large".
 
Mattias Ahlin
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The long data type is a 64-bit signed two's complement integer. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive).

You're right that is can store 9223372036854775807 different numbers. Since half of them are negative, only half of them can be positive.
 
David Nemeskey
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mattias: the explanation is great, but I guess you wanted to write

"You're right that is can store 18446744073709551616 different numbers. Since half of them are negative, only half of them can be positive."
 
RajendraKr Kumar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for Reply,
But how can i store this big value(18446744073709551615) in long datatype.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16026
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot, because that number falls outside the range of the long data type.
 
Kaydell Leavitt
Ranch Hand
Posts: 690
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you need 64 bits or more without the number being negative, you can use the class:

BigInteger

-- Kaydell
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!