• 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

Bit Pattern

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check this thread.
 
Nikki Freeman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic