This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes kindly explain the code. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "kindly explain the code." Watch "kindly explain the code." New topic
Author

kindly explain the code.

ramanuja varun
Ranch Hand

Joined: Aug 31, 2007
Posts: 47
public class Test
{
public static void main(String args[])
{
int x = 11;

x &= 2; //1

System.out.println(x << 2 + x); //2
}
}
please explain why this code will print 32 instead of 10(x<<2 will give 8)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 44605
    
  34
Please put all your code in CODE tags; it makes it much easier to read.

It is impossible for anything & 2 to equal 1; in fact 11 & 2 gives 2. The + operator has a higher priority than the bit shift operators, so you are shifting by 4, not by 2.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: kindly explain the code.
 
It's not a secret anymore!