-3

Is there any mathematical formula for X >>> Y. We have formula for X >> y and X << Y ? If sombody says what is the value of 5 >>> 2, how will you calculate the value without converting them in to binary.

Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
Sambit
  • 7,625
  • 7
  • 34
  • 65

1 Answers1

1

>>> is a logical or unsigned right shift.

Unlike >>, which is an arithmetic right shift (and respects signs), >>> treats the left operand more as an unsigned value -- or a sequence of bits -- than a normal number. It's the bits that matter with a logical shift, not the signed value they represent in Java. So a purely mathematical formula is somewhat pointless, IMO.

If you insist on it, though, the bits of any int a -- even negative numbers -- are the same as the lower 32 bits of a+232. So for a >>> b, you could find it as ((a+232) mod 232) / 2b.

The big problem you'll run into is that 232 (and by extension, the sum of it and any non-negative integer) won't fit in an int. You'll need to upsize your values by using long (or double) in places. And if a is a long, then you'll need to learn a bit about BigInteger. (I'm not even going to bother covering that.)

cHao
  • 84,970
  • 20
  • 145
  • 172