• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

shift ops!

 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
a simple ?, if the right hand operand in a bit-shift expression is negative(e.g. 10 >> -2), then is there any separate rule for such calculations?
ashok.
 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The shift distance for integer values is always in the range
of 0 to 31 and for long values in the range of 0 to 63
.So you cannot possibily have a negative shift distance !
Correct me if wrong

 
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think shifting by negative value is actually shifting by their positive counterparts. For instance, 2>>-1 actually equals 2>>1111 1111 1111 1111, which eqauls 2>>31.
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's assume the right hand side operand is R, the left side operand is L.
For shift operation L SHIFT R, There will be a % operation if R is not inside the given range.
If L is int, the range is 0-32.
If L is long, the range is 0-64.
In that case, the result of the following
If L is int, R%32
If L is long, R%64

will be replaced for R.
Hope it helps.
------------------
Guoqiao Sun
Sun Certified Programmer for Java™ 2 Platform
Try my mock exam¹ at my homepage.
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the actual operation carried out on the right hand operand is a bitwise & using a mask of 0x1F for ints and 0x3F for longs. For positive values this is the same as a %32 or %64, but for negative values it behaves differently. What it amounts to, at least in the few tests I've done, is that if the operand is greater than -32(for ints) it adds the operand to 32. So a shift of -4 would actually be a shift of 28. If the operand is less than -32 it will do a %32 on the operand then add the result (a negative number) to 32.
For a line like this:
54 >>> -34;
take -34%32, the result is -2, add -2 to 32, the result is 30, so the line could be written as:
54 >>> 30;
As a follow up, I dont think you'll see anything like this on the test - I didn't and dont know of anyone else who did.
hope this helped


------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
 
reply
    Bookmark Topic Watch Topic
  • New Topic