I am wondering how to implement a circular right shift by k of the bitstring represented by the int bits.
public int r
The answer by schnaader is correct:
return (bits >>> k) | (bits << (32-k));
(bits >>> k) right-shifts the value stored in bits by k bits and 'the third >' ensures that the leftmost bit is a zero instead of the sign of the bits(bits << (32-k)) left-shifts the value in bits by k-complement number of bitsNow, you have two temporary variables where the first (32-k) bits are stored on the rightmost bits of var (1), and the last k bits are stored on the leftmost bits of var (2). The bitwise or operation simply ORs these two temp vars together (note the use of >>> instead of >>) and you have the circular shift.