Java中的整数rotateRight()方法

位移位用于编程,在每种编程语言中至少有一种变体。Java只有一个逻辑右移运算符(>>)。位移位是一种按位操作。位移位是在二进制值的所有位上进行的,它将位向右移动一定数量的位置。如果值为0100;(即4)右移,变为0010;(即2)再次向右移动,则变为0001;或者1。 爪哇。整型。rotateRight()是一种方法,它返回通过旋转指定int值的两个补码二进制表示形式得到的值 A. 按指定的位数向右移动。位移向右侧,即较低阶。

null

语法:

public static int rotateRight(int a, int shifts)

参数: 该方法采用两个参数:

  • A. :这是整数类型,指要对其执行操作的值。
  • 转移 :这也是整数类型,指的是旋转距离。

返回: rotateRight()方法返回通过将指定int值的两个补码二进制表示形式向右旋转指定位数而获得的值。

下面的程序演示了Java。整型。rotateRight()方法: 项目1: 对于一个正数。

// Java program to illustrate the
// Java.lang.Integer.rotateRight() method
import 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++;
}
}
}


输出:

16
4
1

项目2: 对于负数。

// Java program to illustrate the
// Java.lang.Integer.rotateRight() method
import 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
-1073741835
1879048189

© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享