Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Bit Pattern

 
Nikki Freeman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check this thread.
 
Nikki Freeman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic