Armless Right Shift Ruby

I have a piece of Java code: return (int)(seed >>> (48 - bits));

As you can see, it uses the unsigned right shift operator (β†’>). I am trying to implement this code in ruby, which does not have an unsigned right shift operator, only a signed right shift operator. Since I am not very familiar with the operator β†’>, I am not quite sure how I will impelemnt it in ruby. I tried to do some searches to find out if anyone had encountered this problem before, but could not find anything suitable. Any help would be greatly appreciated :)

+1
source share
1 answer

The unsigned shift operator can be easily implemented using simple bit shifting and masking:

 public static int unsignedShift(int amt, int val) { int mask = (1 << (32 - amt)) - 1; return (val >> amt) & mask; } 

The mask works by setting all the bits to 1, which should be kept after the shift. Note that this returns different results compared to Java for amt> = 32 and amt = 0.

+2
source

All Articles