This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Shift operators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Shift operators" Watch "Shift operators" New topic
Author

Shift operators

Amit, Jhalani
Greenhorn

Joined: Jan 11, 2001
Posts: 21
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

Joined: Jun 13, 2000
Posts: 3340
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 Hope This Helps
Carl Trusiak, SCJP2, SCWCD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Shift operators
 
Similar Threads
shift operators
Problem in right shift Operator
Shifting Operators?
shift by negative number
right shift