aspose file tools*
The moose likes Beginning Java and the fly likes binary example Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "binary example" Watch "binary example" New topic
Author

binary example

Mathew Lee
Ranch Hand

Joined: Jun 08, 2009
Posts: 238
>>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.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13868
    
  10

When you write an integer literal starting with a 0 in Java, such as 010 in the code above, then the number is interpreted as an octal number (base 8) instead of a normal, decimal number (base 10).

010 in octal = 8 in decimal (read about octal if you don't understand why).

The | operator is the bitwise OR operator.

So: 010 | 4 = 8 | 4, in binary this is: 1000 | 0100 = 1100, which is 12 in decimal.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: binary example
 
Similar Threads
any one come up with a solution
Code to generate a Grid Test GUI
Alice in Wonderland