• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

help needed for the shift operators

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bill,
Can you post your program? I want to see your code.
Thanks!
Zheng
 
Priya Krishnan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another resource is the Cat and Mouse Games with Bits It can help give you a basic understanding
 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
// 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 [email protected] in event of problems
 
Priya Krishnan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thank you very much Bill,Rajpal & Carl.
I will all this.
Thanx
Priya
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic