# help needed for the shift operators

Priya Krishnan
Greenhorn
Posts: 7
Hi all,
With <<, >>,>>>
is there any thum rule on how to perform the calulations as below
-1>>>129
12<<89<br /> 13>>-67
Regards,
Priya

William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13074
6
The main thing is that Java never shifts more than 31 for int operations or 63 for long operations.
For int, the low 5 bits of the shift are masked off and this number is the actual number of shifts.
I can send you a program to experiment with shifts -
Bill

------------------
author of:

Zheng Huang
Ranch Hand
Posts: 49
Hi Bill,
Thanks!
Zheng

Priya Krishnan
Greenhorn
Posts: 7
Hi William,
I have R& H and Mughal. They have not given specific examples of this type. It will be helpful for me if you can post some programs on the all three operation. Or can you please suggest me some sites where I can get the concept strongly.
Regards
Priya

Carl Trusiak
Sheriff
Posts: 3341
Another resource is the Cat and Mouse Games with Bits It can help give you a basic understanding

Rajpal Kandhari
Ranch Hand
Posts: 126

Hello Zheng & Priya,

Try the following code:
class ShiftTest {
public static void main(String args[]) {
int a = -1, b = 129, c = 12, d = 89, e = 13, f = -67;
long la = -1, lb = 129, lc = 12, ld = 89, le = 13, lf = -67;
int A = a >>> b;
int B = c << d;<br /> int C = e >> f;
System.out.println("-1 >>> 129 = "+ A);
System.out.println("12 << 89 = "+ B);<br /> System.out.println("13 >> -67 = "+ C);
long AA = la >>> lb;
long BB = lc << ld;<br /> long CC = le >> lf;
System.out.println("-1 >>> 129 = "+ AA);
System.out.println("12 << 89 = "+ BB);<br /> System.out.println("13 >> -67 = "+ CC);
}
}
Hope your doubt will be clear. But still if you have to ask some question on the output then fell free to ask.

------------------
Regards,
Raj.
-------------------------
Afforts should be Appriciated.
-------------------------

William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13074
6
// simple console style Java application Shifter.java
// from Java 2 Exam Prep
// run with two numbers on the command line
// the first number is shifted by the second with each operator
public class Shifter extends Object {
public static void main(String[] args){
if( args.length < 2 ){<br /> System.out.println("Shifter expects to find two integers on the command line" );<br /> System.exit(0);<br /> }<br /> try {<br /> int a = Integer.parseInt( args[0] ); <br /> int b = Integer.parseInt( args[1] );<br /> System.out.println("Operands: " + Integer.toBinaryString( a ) + " shift= " + b ) ;<br /> int c = a << b ;<br /> System.out.println("Result << " + Integer.toBinaryString( c ) + " = " + c ) ;<br /> c = a >> b ;<br /> System.out.println("Result >> " + Integer.toBinaryString( c ) + " = " + c ) ;<br /> c = a >>> b ;
System.out.println("Result >>> " + Integer.toBinaryString( c ) + " = " + c ) ;
}catch(NumberFormatException ex ){
System.out.println("Exception " + ex + " in " + args[0] );
}
System.exit(0);
}
}
// watch out for wrapped lines
// write me at wbrogden@lanw.com in event of problems

Priya Krishnan
Greenhorn
Posts: 7

Thank you very much Bill,Rajpal & Carl.
I will all this.
Thanx
Priya

 Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters?