Looks like a simple method, but I can't understand how |
works here:
/**
* Bitwise rotate a 32-bit number to the left.
*/
private static int rotateLeft(int x, int n) {
return (x << n) | (x >>> (32 - n));
}
First part (x << n)
, where <<
is binary left shift operator. The left operands x
value is moved left by the number of bits specified by the right operand n
.
The (x >>> (32 - n)) part, where >>>
is shift right zero fill operator. The left operands x
value is moved right by the number of bits specified by the right operand (32 - n)
and shifted values are filled up with zeros.
But what is the purpose of |
?
EDIT:
OK, I found that it is simple operator: "binary OR operator copies a bit if it exists in either operand". So the sequence is: (x << n)
, then (x >>> (32 - n))
, then (left part result) | (right part result)
?