This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Simple Problem (9 & 5)  RSS feed

 
Pankaja Shinde
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class SimpleClass
{
public static void main(String[] args)
{
System.out.println("Hello World!" + (9 & 5));
}
}

Will this class compile ?
Explain.
What is the value of (9 & 5) ? How to calculate ?

Pankaj Shinde
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this context, & is the bitwise and operator.

9 = 1001
5 = 0101

9 & 5 = 0001
 
Prasad Shindikar
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
& operator is used as a binary operator in this case.
Hence the & operation will be done as shown above by Keith
and since the result is 1, the output should be

HelloWorld!1
 
Bert Bates
author
Sheriff
Posts: 8945
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pankaja,

In addition to the other good feedback you've gotten here, remember that bit manipulations like this one are a part of the 1.4 exam, but if you're studying for the 5.0 exam (which I'd recommend), you don't need to study any form of bit manipulation.

hth,

Bert
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!