JDK源码之Integer类——rotateRight()方法
  TEZNKK3IfmPf 2023年11月13日 28 0

rotateRight()方法的功能就是将传入的整数按照二进制位循环右移指定位数。

如图:

JDK源码之Integer类——rotateRight()方法

该方法的源码如下:

    /**
     * Returns the value obtained by rotating the two's complement binary
     * representation of the specified {@code 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.)
     *
     * <p>Note that right rotation with a negative distance is equivalent to
     * left rotation: {@code 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: {@code rotateRight(val,
     * distance) == rotateRight(val, distance & 0x1F)}.
     *
     * @param i the value whose bits are to be rotated right
     * @param distance the number of bit positions to rotate right
     * @return the value obtained by rotating the two's complement binary
     *     representation of the specified {@code int} value right by the
     *     specified number of bits.
     * @since 1.5
     */
    public static int rotateRight(int i, int distance) {
        return (i >>> distance) | (i << -distance);
    }

对该方法进行注释,如下:

    /**
     * 返回通过将指定的int值的二进制补码二进制表示右旋转指定的位数获得的值。 (位从右手或低阶移出,左侧重新进入,或高阶移出。)
     * 请注意,负距离的右旋等效于左旋: rotateRight(val, -distance) == rotateLeft(val, distance) 。 还要注意,以32的任意倍数旋转是空操作,因此,即使距离的最后五个位为负数,也可以忽略所有旋转距离,即使该距离为负: rotateRight(val, distance) == rotateRight(val, distance & 0x1F) 。
     * 例如987654321的二进制是                    0011 1010 1101 1110 0110 1000 1011 0001
     * 调用rotateRight(987654321,3)方法后二进制是 0010 0111 0101 1011 1100 1101 0001 0110
     *
     * @param i        要向右旋转其位的值
     * @param distance 向右旋转的位的数量
     * @return 通过将指定的int值的二进制补码二进制表示右旋转指定的位数获得的值。
     */
    public static int rotateRight(int i, int distance) {
        /*
            例如:i=987654321, distance=3
            i                                       0011 1010 1101 1110 0110 1000 1011 0001
            i >>> distance                          0000 0111 0101 1011 1100 1101 0001 0110
            i << -distance                          0010 0000 0000 0000 0000 0000 0000 0000
            (i >>> distance) | (i << -distance)     0010 0111 0101 1011 1100 1101 0001 0110
         */
        // 在移位的时候,如果distance小于0,会根据被移位数的长度进行转换。就比如说这里我们对long进行移位,那么-distance就会被转换成(64 + distance)(注,这里的distance是小于0的)。
        return (i >>> distance) | (i << -distance);// (i << -distance)等价于(i << 32-distance),注意是因为int是32位,long是64位的
    }

循环右移过程如下:

JDK源码之Integer类——rotateRight()方法

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月13日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   21天前   48   0   0 java
  TEZNKK3IfmPf   2024年05月31日   27   0   0 脚本jdk
  TEZNKK3IfmPf   2024年05月31日   55   0   0 java
TEZNKK3IfmPf