• Post Reply Bookmark Topic Watch Topic
  • New Topic

How many digits an octal number have ?  RSS feed

 
ashwin bhawsar
Ranch Hand
Posts: 62
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As per the topic http://www.coderanch.com/t/452169/java/java/Literals#2013737
an Octal number can have up-to 21 digits , but i tried out this code snippet :



It gives following compilation error :

Example.java:9: integer number too large: 0111111111111111111111
long a=0111111111111111111111;

Can anyone explain me how do we calculate how many digits a number can hold ?
And is there any relation between the range(max value) and length(max no. of digits) of a number ?
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The number of digits isn't relevant, it's the value. The value of any int can be between -(2^31) and 2^31 - 1. It doesn't matter if you represent these values as decimal, hexadecimal or octal, those are the bounds.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The thing is that the octal number in the code is actually an int. What you want is:
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or possibly
but then your co-workers may be forced to kill you.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want the number to be a long, it must be followed by an L. [Avoid l even though the compiler will take it happily.]

8 is 2^3, a long goes up to 2^63 - 1, so you divide 3 into 63 and add one for the remainder and you can work it out. Or you can look in the Java™ Language Specification.
 
ashwin bhawsar
Ranch Hand
Posts: 62
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for your replies.

@Rob Spoor : I'll give a thought on "The number of digits isn't relevant, it's the value" and will get back soon with more queries
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!