Win a copy of Kotlin in Action this week in the Kotlin forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Bit Pattern

Nikki Freeman
Greenhorn
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
Posts: 2023

Nikki Freeman
Greenhorn
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
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.

Keith Lynn
Ranch Hand
Posts: 2409
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.

 It is sorta covered in the JavaRanch Style Guide.