• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Hexadecimal Question

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm more of a knucklehead because programming takes longer to register for me so please be patient if the following question is so easy for those java masters.

In K&B Study book they say the following
class HexTest{
public static void main (String[]args){
int x = 0X0001;
int z = OxDeadCafe;
System.out.println("x = " + x + " y= " + y + " z = " + z);
}
}
They say when you run HexTest it produced the following output:
x = 1 y = 2147483657 z = -559035640

My problem is that I am having a hard time understanding how they do the conversion (i.e. how do you get 0x0001 = 1?) and was wondering if anyone could help or direct me to sites where they explain how the conversion is done. Any help would be appreciated.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try reading this.
 
author
Posts: 8998
19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Matt,

A minor point is that you missed a line of code in the example:

int y = Ox7fffffff;

Anyway, I think your question will be answered later in the book when shift operators are discussed

If you can't wait, jump ahead to about page 168...

-Bert
 
Matt Henderson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you both. You have now un-locked my brain. I really do appreciate it. I guess there is hope for every type of programmer Thanks once again.
 
Matt Henderson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This message is for Corey;

Your page really helped me out. My stumbling block is on page 2 where I don't understand how you arrive to those numbers. My apologies if it takes me longer to understand You said the following on Page 2

This is the section in Page 2 talking about shortcuts to convert from one base to another.
Let's look at a couple of examples
Let's convert 0723 (octal) to binary
7 2 3
111 010 011
The octal value 0723 is equivalent to the binary value of 111010011. If you could furhter explain what process you did to convert it I would appreciate it.

Thanks.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, this shortcut is fairly mathematical so, if I explain poorly, I'm sorry.

Anyway, think about the bases that we're working in. Octal is base 8. 2^3 is equal to 8. What this boils down to is that every digit in base 8 is equivalent to 3 digits in base 2, or binary.

Think about what digits are available in octal: 0 through 7. That's a total of 8 digits. In order to represent 8 distinct values in binary, you need 3 digits. With 3 binary digits, you can come up with 8 distinct values, as follows:



Because 8 is a power of 2, we can use this shortcut to directly translate from base 8 to base 2. No such luck with base 10 numbers because 10 is not a power of 2.

So, in the end, when you want to convert from octal to binary, you can convert each digit individually according to the table above. Any time you run across a 4, for example, you replace it with 100. Likewise, if you run into a 5, you replace it with 101. So, if you have the octal value:

02637

You can quickly convert that to binary by converting each digit individually. In the end, you'd get this:

010 110 011 111

Converting from base 16 to base 8 is just as easy. The only difference is that, because 16 is equal to 2^4, each hexadecimal digit is equal to 4 binary digits, as opposed to 3 for octal.

I hope that helps,
Corey
 
Matt Henderson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I bought you a beer for how you have opened up my hard head I would be broke! Thank you so much for breaking it down. I truly appreciate your help in this matter.

Regards,
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Matt Henderson:
If I bought you a beer...



Did someone say beer? You know I'm from Wisconsin, right?
 
Matt Henderson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm psychic what do you know. I love the java programming language but it takes longer for me to understand then others.. it gets frustrating but eventually I get around to understanding it....... I don't want to do the exam and then lose points cuz i overlooked the whole hex-octal stuff which I'm sure they will ask....
 
Bert Bates
author
Posts: 8998
19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, the hex / octal stuff is very minor on the exam. It's important to understand the concept of two's complement, and it's somewhat important to understand the 3 shift operators.

If your goal is to pass, focus on inner classes, threads, and GC. Those are the trickiest topics, and they're all covered heavily.

If your goal is to get 100%, then the hex / octal stuff has to be on the list, but I promise it's a very small part of the exam, in other words it's pretty low value for your studying time.

- Bert
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Bert Bates:
If your goal is to get 100%, then the hex / octal stuff has to be on the list, but I promise it's a very small part of the exam, in other words it's pretty low value for your studying time.



That's good advice from Bert, but I always encourage people to study the bit stuff. Not only will it make you a more knowledgable programmer, but I think that, once you get your head into it (which doesn't take as long as you think), you're all set to pick up a few easy points on the exam.
 
I wasn't selected to go to mars. This tiny ad got in ahead of me:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!