Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes shift question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "shift question" Watch "shift question" New topic
Author

shift question

dlc
Greenhorn

Joined: Feb 17, 2004
Posts: 3
hi all,
i do'nt know what differences between Sample1 & Sample2.
code 1:
class Sample1{
public static void main(String args[]){
int i=-1;
i=i<<-2;
System.out.println(i);
}
}
code 2:
class Sample2{
public static void main(String args[]){
int i=-1;
i=i<<2;
System.out.println(i);
}
}
thank you!!
Igor Ko
Ranch Hand

Joined: Jun 24, 2002
Posts: 90
try it, it will help:


If the promoted type of the left-hand operand is int, only the five lowest-order bits of the right-hand operand are used as the shift distance. It is as if the right-hand operand were subjected to a bitwise logical AND operator & (�15.22.1) with the mask value 0x1f. The shift distance actually used is therefore always in the range 0 to 31, inclusive.

Java Language Specification.
So actually:
shfit -1 is shift to 31
shift -2 is shift to 30
shift -3 is shift to 29
shift -4 is shift to 28
. . . . . . . . . . . . .
[ March 15, 2004: Message edited by: Igor Ko ]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
dlc,
Welcome to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
Consider Paul's rocket mass heater.
 
subject: shift question