Search code examples
javacryptographygost28147

meaning of the line: buf = ((buf << 11) | (buf >>> 21));


I use this code for Cryptography home work assignment and can't understand line# 59

thanks for the helpers.


Solution

  • It's the same as; rotate left by 11 bits for an int value.

    The bottom 21 bits move up 11 while the top 11 bits move down by 21.

    It is a common pattern used in hashing as it quickly rearranges the bits of a number without losing randomness.

    Source for Integer.rotateLeft

    Returns the value obtained by rotating the two's complement binary representation of the specified int value right by the specified number of bits. (Bits shifted out of the right hand, or low-order, side reenter on the left, or high-order.)

    Note that right rotation with a negative distance is equivalent to left rotation: rotateRight(val, -distance) == rotateLeft(val, distance). Note also that rotation by any multiple of 32 is a no-op, so all but the last five bits of the rotation distance can be ignored, even if the distance is negative: rotateRight(val, distance) == rotateRight(val, distance & 0x1F).

    Returns: the value obtained by rotating the two's complement binary representation of the specified int value right by the specified number of bits.

    public static int rotateRight(int i, int distance) {
        return (i >>> distance) | (i << -distance);
    }