Hi all, With <<, >>,>>> is there any thum rule on how to perform the calulations as below -1>>>129 12<<89<br /> 13>>-67 Please help me, Thanx in advance Regards, Priya
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12265
1
posted
0
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
Hi Bill, Can you post your program? I want to see your code. Thanks! Zheng
Priya Krishnan
Greenhorn
Joined: Jan 16, 2001
Posts: 7
posted
0
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. Please help me. Thanx in advance Regards 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. -------------------------
Regards,<P>Raj.<BR>-------------------------<BR>Afforts should be Appriciated.<BR>-------------------------
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12265
1
posted
0
// 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
Joined: Jan 16, 2001
Posts: 7
posted
0
Thank you very much Bill,Rajpal & Carl. I will all this. Thanx Priya