aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes help needed for the shift operators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "help needed for the shift operators" Watch "help needed for the shift operators" New topic
Author

help needed for the shift operators

Priya Krishnan
Greenhorn

Joined: Jan 16, 2001
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
Please help me,
Thanx in advance
Regards,
Priya
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12682
    
    5
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:


Java Resources at www.wbrogden.com
Zheng Huang
Ranch Hand

Joined: Dec 20, 2000
Posts: 49
Hi Bill,
Can you post your program? I want to see your code.
Thanks!
Zheng
Priya Krishnan
Greenhorn

Joined: Jan 16, 2001
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.
Please help me.
Thanx in advance
Regards
Priya
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
Another resource is the Cat and Mouse Games with Bits It can help give you a basic understanding


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Rajpal Kandhari
Ranch Hand

Joined: Aug 26, 2000
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.
-------------------------


Regards,<P>Raj.<BR>-------------------------<BR>Afforts should be Appriciated.<BR>-------------------------
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12682
    
    5
// 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

Thank you very much Bill,Rajpal & Carl.
I will all this.
Thanx
Priya
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: help needed for the shift operators
 
Similar Threads
return value
Majji Exm1 :
Need Guidance
octal, hex
what is your dream?