Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

shift question

 
dlc
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic