My simple question is why:
System.out.println(010|4);
prints \"12\"? I understand bitwise OR operator but why \"010\" equals 8? It\'s defin
As everybody mentioned here that 010
is an Octal Integer literal . The leading 0
specifies that it is an octal representation . Actual value will be :
1*8^1 + 0*8^0 = 8(decimal) = 1000(binary-only last 4 digits)
Now coming back to the SOP :
System.out.println(010|4);
Applying Bitwise OR on 010
and 4
(considering only the last 4 digits) =>
1000|0100
= 1100
= 1*2^3 + 1*2^2 + 0*2^1 + 0*2^0
= 8 + 4 + 0 + 0
= 12(decimal)