• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how many digits are allowed in a HEX number

 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Sun's "Java 2 Certificate" study guide says on p. 13:
a Hexadecimal literals is allowed up to 16 digits, not including the prefix 0x or the optional suffic extension L. What does that reall mean? The following seems to contraditct with that statement:
int y = 0x00000000000000001; // compiled OK, although there are 17 digits
long x = 0xaaaaaaaaaaa; // does not compile, although there are only 13 digits.
I guess how many digits there can be really depends on whether this HEX number is assgined to an integer or long, which has range. The compiler checks to make sure that the hex literal is not out of range?
Thanks.
Yan
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I guess how many digits there can be really depends on whether this HEX number is assgined to an integer or long, which has range. The compiler checks to make sure that the hex literal is not out of range?


I do not think that is right. The number literals are int by default unless you sufix them with L for long. So:
0x0000000001 is by itself an int.
0xaaaaaaa is by itself an int.
0xaaaaaaaL is a long.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

a Hexadecimal literals is allowed up to 16 digits, not including the prefix 0x or the optional suffic extension L. What does that reall mean?


I think, if I am not wrong, that it has something to do with max 8 bytes; that is 16 digits for hex.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
-------------------------------------------------------------------------
a Hexadecimal literals is allowed up to 16 digits, not including the prefix 0x or the optional suffic extension L. What does that reall mean?
--------------------------------------------------------------------------
max limit of hax literal 0x7fffffffffffffff (0x 16 digits)

-------------------------------------------------------------------------
int y = 0x00000000000000001; // compiled OK, although there are 17 digits
-------------------------------------------------------------------------
max limit of int data type is 0x7fffffff;
when u write it as
int y2 = 0x7fffffff; // works, max value for int (with in range).
int y = 0x00000000000000001; // works, value with in range.
int y3 = 0x7fffffff1; // does not work, value out of range.
00010 < 100 (5 digit no is smaller than 3 digit)
int i = 0x000000000000000000000000000000000000000000000000000007fffffff;
int ii = 0x7fffffff;
i == ii == max limit of int dtat type.
-----------------------------------------------------------------------
long x = 0xaaaaaaaaaaa; // does not compile, although there are only 13 digits.
-----------------------------------------------------------------------
default data type of interger value is int with max limit 0x7fffffff
and 0xaaaaaaaaaaa excced the limit of int data type. 0x7fffffffffffffffL or 0xaaaaaaaaaaaL is treated as long.
long x = 0xaaaaaaaaaaaL; is valid.
[ September 23, 2003: Message edited by: MSanjeevMehra ]
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How many digits a Hex number can take is really defined by the size of the variable in bytes. Here I should mention that by digits I mean significant leading (not zeroes) digits, e.g.: 0xabcdef. Every HEX digit in bit representation takes up 4 bits, e.g.: F=1111, A=1010. So, having int variable of size 4 bytes or 32 bits, we divide 32 by 4 and get 8 significant HEX digits limit for int variable. In the same fashion, long is 8 bytes or 64 bits, divide it by 4 and get 16 significant HEX digits - your limit for long integer numbers in HEX. Try

and you should get something like 7fffffffffffffff.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic