1

Can someone explain this java bitwise operator behavior??

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

Thank you!

Sirko
  • 72,589
  • 19
  • 149
  • 183
Rafael
  • 2,521
  • 2
  • 33
  • 59

1 Answers1

6

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.)

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Sirko
  • 72,589
  • 19
  • 149
  • 183
  • Also it is related to http://stackoverflow.com/questions/565634/integer-with-leading-zeroes ... but i didn't figure that were the reason till now. Thank you. – Rafael Feb 10 '13 at 15:38