• 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 operator....

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int i = 1;
i <<= 31;
i >>= 31;
i >>= 1;
int j = 1;
j >>= 31;
j >>= 31;
System.out.println("i = " +i );
System.out.println("j = " +j);
The answer to this when I execute is
i = -1
j = 0
How do I solve this without running the code??? Are there any thumb rules ???
Thanks
Praveen
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll give it a shot, here are my rules
<<, >>, >>> shifted 31 times if it's positive is 0
-# >>> shifted 31 times is 1
<<, >>, >>> shifted 32 times is the same number positive or negative
-1 >> any # = -1
-1 << any number = flip the bits + 0001 then shift. to get the decimal number back, flip the bits and add one again.
Please correct my if I am wrong, but I believe I am correct.
Also, these rules only apply for int's, you need to use 64 for long values.
-Matt
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My 2 cents:
1 << -2 is equiv to 1 << (32 - 2). Dunno why? Could someone explain?
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Matt,
i << 31 is not always zero if x is a +ve int.
Example: i = 1
Eric,
1 << x =
1 << (x mod 32) (if dealing with ints)
N.B. -2 = 30 mod 32.
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Eric Low:
My 2 cents:
1 << -2 is equiv to 1 << (32 - 2). Dunno why? Could someone explain?


Eric
From the
JLS section 15.19

... only the five lowest-order bits of the right-hand operand are used as the shift distance. It is as if the right-hand operand were subjected to a bitwise logical AND operator & (�15.22.1) with the mask value 0x1f. The shift distance actually used is therefore always in the range 0 to 31, inclusive.


So in your example a left shift of -2:
-2 & 0x1F is
11111111 11111111 11111111 11111110
00000000 00000000 00000000 00011111
00000000 00000000 00000000 00011110
is 30
In most cases this is the same (as Jay said) as the right hand side % 32.
hope that helps
 
Stop it! You're embarassing me! And you are embarrassing this tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic