2

my code is

final int CONST_1 = 1010;
final int CONST_2 = 1011;

System.out.println("CONST_1 & CONST_2: " + Integer.toBinaryString(CONST_1 & CONST_2));
System.out.println("CONST_1 ^ CONST_2: " + Integer.toBinaryString(CONST_1 ^ CONST_2));
System.out.println("CONST_1 | CONST_2: " + Integer.toBinaryString(CONST_1 | CONST_2));
System.out.println("~CONST_1 : " + Integer.toBinaryString(~CONST_1));

Output is

CONST_1 & CONST_2: 1111110010
CONST_1 ^ CONST_2: 1
CONST_1 | CONST_2: 1111110011
~CONST_1 : 11111111111111111111110000001101

In my opinion it's wrong and it should be:

CONST_1 & CONST_2: 1010
CONST_1 ^ CONST_2: 1
CONST_1 | CONST_2: 1011
~CONST_1 : 101

Please explain me why I have such result. Thanks!

1
  • 3
    The values you initialized your constants with are DECIMAL, not binary! Commented Dec 11, 2011 at 14:47

5 Answers 5

8

Change this:

final int CONST_1 = 1010;
final int CONST_2 = 1011;

to this:

final int CONST_1 = 0b1010;
final int CONST_2 = 0b1011;

Don't forget that literals are decimal by default. You clearly wanted them to be binary.


Binary literals require Java 1.7. So if that's not available, you can go with this:

final int CONST_1 = Integer.parseInt("1010",2);
final int CONST_2 = Integer.parseInt("1011",2);
Sign up to request clarification or add additional context in comments.

3 Comments

I can't change it to final int CONST_1 = 0b1010;
it says Binary literals can only be used with source level 1.7 or greater
See this question: (stackoverflow.com/questions/867365/…) the 0b prefix requires Java 7.
1

I think you know what is meant by a literal. If not, please refer: Java Literals and Literal.

Now, Integer and Floating-Point literals are decimal by default in Java. So, the value 1010 you typed above will be decimal 1010. ie., One thousand and ten. If you want them to be binary (it is clear from the question), there are many possibilities.

  • Use equivalent decimal value.

You may use the decimal equivalent of the binary value you want to represent. Here, for example, decimal equivalent of binary 1010 is 10, and that of binary 1011 is 11.

final int CONST_1 = 10;
final int CONST_2 = 11;
  • Use wrapper classes parse method

Each wrapper class has a parse method, which takes the base of number system also as argument. So

final int CONST_1 = Integer.parseInt("1010", 2);
final int CONST_2 = Integer.parseInt("1011", 2);
  • If you are using Java 7, use binary literals

Binary literals are not supported in older versions of Java. Java 7 introduces binary literals. See features.

final int CONST_1 = 0b1010;
final int CONST_2 = 0b1011;

Comments

1

CONST_1 is 1010 in decimal. The binary value of CONST_1 is 1111110010. Similarly CONST_2 is 1111110011.

Does the result make more sense now?

Comments

0

Don't confuse an integer composed of only 0 and 1 with the binary representation of the integer...

The integer 1010 is 1111110010 in binary, so the results are correct.

Comments

0

Your numbers aren't binary. They are written in decimal. You want to prepend a 0b to tell Java 7 this int is in binary. If you aren't using Java 7 there is no binary literal syntax so you can do this Integer.parseInt("1010", 2) as a work around or use HEX literal notation:

final int CONST_1_BINARY = 0b1010;
final int CONST_1_DECIMAL = 1010;

if( CONST_1_BINARY == CONST_1_DECIMAL ) {
   System.out.println("They are the same!");
} else {
   System.out.println("They are NOT the same.");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.