This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Shift operators

 
Amit, Jhalani
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can sombody help me out on this.
public class Test {
public static void main(String b[]){
int n = 3;
n <<= -2;
System.out.println(n);
}
}
I am able to compile it and able to run it also. I wanna know can i use negetive numbers with the shift operatos.The above program gave me the answer as -1073741824.
Please explane.
Thanks
Amit
 
Carl Trusiak
Sheriff
Posts: 3340
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This discussion explains this extremely well http://www.javaranch.com/ubb/Forum24/HTML/000848.html
Note the lower 5 bits of -2 result in a value of 30 so, this is the same as writing
n <<= 30;
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic