• 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

What is the Hexadecimal expression rule?

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In RHE chapter 1, Literals, only integral literal hexadecimal is touched. It is said 28 in Hex is 0x1c.
In Barry Booneexam 1,
Question 6: Which assignments are legal?
Select all valid answers.
a. long test = 012;
b. float f = -412;
c. int other = (int)true;
d. double d = 0x12345678;
e. short s = 10;
Answer are abde.
My opinion: according to answer d,because double is 64 bits, so hexadecimal is 8 numbers not including "0x". If things work like this, the integral literal default is int type, so the hexadecimal for it should be in 4 numbers? But in RHE it is said in 2 numbers. I know RHE is barely wrong, but I am confused here, can anybody clear me out? What is the rule for hexadecimal? Can double and float be expressed in hexadecimal?
Any of your opinion is highly appreciated!!
 
Ranch Hand
Posts: 171
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any long literal can be expressed in hexadecimal. Just pretend that the value is converted to the decimal value, and then it is simply a long literal. Then apply the usual rules of arithmetic promotion.
Geoffrey

------------------
Sun Certified Programmer for the Java 2 Platform
 
Kathy Wang
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any long literal can be expressed in hexadecimal. Just pretend that the value is converted to the decimal value, and then it is simply a long literal. Then apply the usual rules of arithmetic promotion.
Geoffrey
Thanks Geoffrey.
But I am quite clear, can you explain further? Decimal long 1 can be expressed in hexadecimal, so it is expressed as 0x0001 or 0x01? Thanks again.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Each digit in an hex number represents 4 bits so you need 16 hex digits for representing 64 bits.
double d = 0x123456789;
does not compile it's too big for an integer but
double d = 0x1234567890123456L;
it does
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jose,

Originally posted by Jose Botella:
Each digit in an hex number represents 4 bits so you need 16 hex digits for representing 64 bits.
double d = 0x123456789;
does not compile it's too big for an integer but
double d = 0x1234567890123456L;
it does


I'm a little confused on this too.
So would decimal long 1 be Ox0000000000000001L?
Isn't is ok to express a hex number in two or four digits if possible, like Ox0001 or Ox01? I think I have seen that done for integers that would otherwise have lots of leading zeros (like in this case: Ox00000001).
Thanks,
Liz

------------------
Elizabeth Lester
SCJP Dreamin'
 
Kathy Wang
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jose,
But I am confused by your explanation too. Is that what you mean: Barry Boone exam 1, Q6 answer should not include d?
You said "Each digit in an hex number represents 4 bits", do you get the conclusion from Unicode in 4 digits? Could you please explain in detail?
Thanks alot.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
to Elizabeth:
An int has 32 bits so you can placed at most 32bits in a hex integer literal.
0x1
0x0001
they are the same. Just compute them from hex to decimal.
to Kathy:
d answer is ok because it has 32 bits and the type is int.
But 0x123456789 has 36 bits so it's too big for an integer.
Each digit in hex represents up to 4 bits in bynary:
0x0 0000
0x1 0001
0x2 0010
0x3 0011
0x4 0100
0x5 0101
0x6 0110
0x7 0111
0x8 1000
0x9 1001
0xa 1010
0xb 1011
0xc 1100
0xd 1101
0xe 1110
0xf 1111
Please review the binary, hexadecimal and octal systems. They are used in Java.
 
Elizabeth Lester
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jose,

Originally posted by Jose Botella:
to Elizabeth:
An int has 32 bits so you can placed at most 32bits in a hex integer literal.
0x1
0x0001
they are the same. Just compute them from hex to decimal.


Thanks, that is as I suspected. I just couldn't find documentation on it anywhere!
--liz
[This message has been edited by Elizabeth Lester (edited October 01, 2001).]
 
Kathy Wang
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jose. Your explaination do clear me out. Thanks again.
Kathy
 
I can't beleive you just said that. Now I need to calm down with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic