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!

Mysticial
  • 464,885
  • 45
  • 335
  • 332

5 Answers5

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);
Mysticial
  • 464,885
  • 45
  • 335
  • 332
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?

Grzegorz Rożniecki
  • 27,415
  • 11
  • 90
  • 112
nickd
  • 3,951
  • 2
  • 20
  • 24
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;
Jomoos
  • 12,823
  • 10
  • 55
  • 92
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.

Tudor
  • 61,523
  • 12
  • 102
  • 142
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.");
}
chubbsondubs
  • 37,646
  • 24
  • 106
  • 138