Rafael
Rafael

Reputation: 2676

Bitwise operator unexpected behavior

Can someone explain this java bitwise operator behavior??

System.out.println(010 | 4); //  --> 12
System.out.println(10 | 4);  //  --> 14

Thank you!

Upvotes: 1

Views: 104

Answers (1)

Sirko
Sirko

Reputation: 74036

The first number is interpreted as octal. So 010 == 8.

Starting from that, it is easy to see, that

8d | 4d == 1000b | 0100b == 1100b == 12d

The second number is interpreted to be decimal, which yields

10d | 4d == 1010b | 0100b == 1110b == 14d

(Where d indicates a decimal number and b indicates a binary one.)

Upvotes: 6

Related Questions