binary example

>>What will be output by the following line of code?

System.out.println(010|4);

1) 14
2) 0
3) 6
4) 12

Answer given was 12.

 
I read above question from link
http://www.jchq.net/certkey/0503certkey.htm
i did not understand how 12 is answer and how octal is related here.



Any ideas, resources,sample code,links,  highly appreciated. thanks in advance.
LVL 7
gudii9Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

for_yanCommented:

010 is octal ten as it starts with zero, means it is decimal 8 or binary 1000

4 is binary 100

now when you make bitwise or 1000 with 100 you end up with 1100 which will be 8 + 4 = 12

0
Amitkumar PSr. ConsultantCommented:
Agreed with for_yan.

gudii9,

To understand the working process of the & and | operators, you need to be familiar with converting from decimal to binary and learn what happens with the bit patterns.
0
gudii9Author Commented:
>>4 is binary 100
How to know the number orignally in which mode like octal, binary, decimal, Hex etc. Then
how we know we have to convert to binary before adding when there are 10 different modes like Hex, Octal etc all. please advise
0
for_yanCommented:

In Java plain normal numbers are understood of course as decimal,
when the integer starts with zero - that is octal,
when it starts with 0x - that is hexadecimal

I think until Java 7 there is no special way to specify
binary, in Java 7 it should begin woth 0b or 0B, like that:  0b1010000
Before Java 7 you may use Integer.paarseInt("10101",2);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.