aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Bit Pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Bit Pattern " Watch "Bit Pattern " New topic
Author

Bit Pattern

Nikki Freeman
Greenhorn

Joined: Nov 03, 2005
Posts: 10
I am studying for the 1.4 test.

Unfortunately, bit shifting is part of the exam. I have never used bit shifting before. In all of the examples that I have seen (including the K&B book), it is assumed that you know what the bit pattern is for a given integer. .

My question: how are you supposed to know what the bit pattern is for an integer? Is there a formula for this? thnx, Nikki
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
Check this thread.
Nikki Freeman
Greenhorn

Joined: Nov 03, 2005
Posts: 10
Ok, after reading that, I'm even more confused.

For instance:


As the other user suggested, us the toBinaryString method results in:



My other question is; is it worth my time to try to figure this out? Are there very many questions on this for the exam?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Nikki Freeman:
...int i = 00000110;
System.out.println(i); // = 72 not 6...

An integral literal that begins with zero is interpreted as octal (base 8). So 0110 is 64 + 8 = 72.

Similarly, hexadecimal (base 16) literals are prefixed with a zero and the letter 'x', using letters a-f to represent 10-15. For example, 0x1c represents 28.

You should expect bit questions on the 1.4 exam, so you should spend some time getting more comfortable with this.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
The reason you get 72 when you print is that an integer contstant that begins with a 0 is considered an octal constant.

So 00000110 = 1*8^2 + 1*8 = 64 + 8 = 72.

It would benefit you if you learned the process to convert from a decimal to a binary number.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Bit Pattern