Java Integer rotateRight()方法
位移在编程中使用,在每种编程语言中至少有一种变化。Java有一个单一的逻辑右移操作符(>>)。位移动是一种位操作。位移动是在二进制值的所有位上进行的,它将位向右移动一定数量的位置。如果值0100;(即4)被右移,就变成了0010;(即2),再向右移就变成了0001;或1。
java.lang.Integer.rotateRight()是一个方法,它返回我们通过将指定的int值a的二进制补码向右旋转指定位数而得到的值。位数会向右移动,即低阶位。
语法:
public static int rotateRight(int a, int shifts)
参数。该方法需要两个参数。
a : 这是一个整数类型的参数,指的是要对其进行操作的值。shifts:这也是一个整数类型,指的是旋转的距离。返回:rotateRight()方法返回将指定的int值的二进制补码向右旋转指定位数后得到的值。
下面的程序说明了Java.lang.Integer.rotateRight()方法:
程序1: 对于一个正数。
// Java program to illustrate the// Java.lang.Integer.rotateRight() methodimport java.lang.*; public class Geeks { public static void main(String[] args) { int a = 64; int shifts = 0; while (shifts < 3) // It will return the value obtained by rotating left { a = Integer.rotateRight(a, 2); System.out.println(a); shifts++; } }}输出。
1641
程序2: 对于一个负数。
// Java program to illustrate the// Java.lang.Integer.rotateRight() methodimport java.lang.*; public class Geeks { public static void main(String[] args) { int a = -165; int shifts = 0; while (shifts < 3) // It will return the value obtained by rotating left { a = Integer.rotateRight(a, 2); System.out.println(a); shifts++; } }}输出。
-42-10737418351879048189
