• 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:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Shift Operator

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q46
class shift
{
public static void main(String args[])
{
long l = 99;
int i = 1;
int j = 0;
}
}

if 'i' is left shifted by 'l' and the result assigned to 'j',will 'i' undergo an implicitly widening & narrowing conversion from int to long & vice versa or,will 'i' remain as an int and be left shifted by 3(99%32 ie.3).How does it work ?


[This message has been edited by Savio Mascarenhas (edited December 10, 2000).]
[This message has been edited by Savio Mascarenhas (edited December 10, 2000).]
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This doe in class shift would not compile...
1) missing ;
Typo perhaps?
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well your code will not compile, however if u make some changes as below then it will show the result as u said, ie 'i' remain as an int and be left shifted by 3(99%32 ie.3) in "case 1" and after that j will b holding value 3, so i will now b shifted to 3 places giving the result 256.
class shift
{
public static void main(String args[])
{
long l = 99;
int i = 1;
int j = 0;
j=i<<l; // case 1 (99%32 =3)<br /> j=i<<j; // case 2 (j = 1<<3 ie 256)>
System.out.println(j); }
}
I hope i m clear.
 
Simon Cockayne
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The "new" code does not compile.
Can you explain what you think is going on in the line...
j=i<j=i<System.out.println(j);<br /> I thought < was a comparison operator returning true or false.<br /> The shift operators are << >> or >>>.
So I don't see what is supposed to be occuring.
Also, why is l delcared and not used?
 
Simon Cockayne
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this a problem with using greater than and smaller than signs in a HTML representation of the text?
The message I have just posted and presumably the previous two messages do not appear as they were intended.
How is one supposed to represent the shift operators in a reply.
Ta.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
whatever may be the case with HTML or UBB, here is my explanation. I am not posting the code for the fear of it not appearing properly and my reply going waste.
Shift Operators don't do an automatic type promotion, instead they depend on unary promotion of the left hand argument of the shift operator.
If the left hand operand of the shift operator is int, then it does a %32 of the right hand operand and does the shift, if the left hand operand is long, then it does a %64 of the right hand operand and does the shift. Hence in your code,
i left shifted by 99 would give 8 as the result, since
i is the left hand operand of this shift and its type is int. Hence, 99%32=3 would be done and 1 left shifted by 3 is nothing but 8.
Hope it is clear.
Regds,
Aparna
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it will implicitly do the widening conversion .and will change it to long type .
i am not able to get what do you mean by (99%32=3).please explain.
 
Savio Mascarenhas
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by amit goel:
it will implicitly do the widening conversion .and will change it to long type .
i am not able to get what do you mean by (99%32=3).please explain.


Amit,
'%' is the modulus operator(ie.remainder).If the LHS operand is an int and the RHS operand is larger than 32(size of int)then,the LHS operand is shifted by (RHS operand %32).Similarly,when the LHS operand is a long and the RHS operand is larger than 64(size of long) then,the LHS operand is shifted by (RHS operand % 64).
For instance,the expression 1<<34 would be evaluated as 1<<2(34%32=2 ie. 34 divided by 32 leaves a remainder of 2).

 
Live a little! The night is young! And we have umbrellas in our drinks! This umbrella has a tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic