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.

- 36,091
- 7
- 95
- 123

- 7,625
- 7
- 34
- 65
-
I thought they had >>> in Java for this – barak manos Jan 16 '14 at 14:10
-
they do have it - http://stackoverflow.com/questions/14501233/unsigned-right-shift-operator-in-java – Caffeinated Jan 16 '14 at 14:10
-
1Any explanation of **bit** shifting, that doesn't mention **bits**, is rather useless IMO. – cHao Jan 16 '14 at 14:16
1 Answers
>>>
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.)

- 84,970
- 20
- 145
- 172