• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

binary example

 
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>>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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
That's my roommate. He's kinda weird, but he always pays his half of the rent. And he gave me this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic