Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A problem with java literals(Hex & octal values etc)

 
Femi Alla
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I would be sitting for the programmer exam on saturday and there is only one set of questions I would skip 'cos I don't know. It's
a little problem with the declaration of octal and hexadecimal values in java.
The Java 2 certification study guide says on page 11 that
"The value of 28 may thus be expressed six ways:
28// Decimal value.
034//this is supposed to be in base 8,right?
0x1c}
0x1C} Hexadecimal values.
0X1c}
0X1C}
No matter how hard I've tried, I don't seem to understand any except for the first. My calculation of 28 to base 8(the second item on the list) is: 2x81 + 2x80
Which would give; 16 + 2. I'd have expected the result to be 018. But it's 034. The rest I don't even know what to think.
I need help here. Can anyone please help me out?
Thanks.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've got a few things turned around. It's 34 which is already in octal - so to see the equivalent decimal value, calculate
3 * 8^1 + 4 * 8^0 = 24 + 4 = 28
(where of course ^ is "to the power of", not the java OR operator) Likewise 0x1C is in hex, so convert to decimal thus:
1 * 16^1 + 12 * 16^0 = 16 + 12 = 28
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On each bite,
1~0 for digital
1~7 for octal
1~10~A~B~C~D~E for hex
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic