right() java_Java BigInteger shiftRight()用法及代码示例

java.math.BigInteger.shiftRight(int n)方法返回一个BigInteger,其值为(this >> n)。移位距离n可以为负,在这种情况下,此方法执行左移。 shiftRight()方法会将数字的二进制表示形式中的每个数字右移n次,并且将移位方向的最后一位替换为0。此shiftRight()方法计算底数(this /2^n)。

用法:

public BigInteger shiftRight(int n)

参数:该方法采用整数类型的一个参数n,该参数n以位为单位。

返回值:该方法将位右移n次后返回BigInteger。

异常:如果移位距离为Integer.MIN_VALUE,则该方法可能会引发ArithmeticException。

例子:

Input: BigInteger = 2300, n = 3

Output: 287

Explanation:

Binary Representation of 2300 = 100011111100

Shift distance, n = 3.

After shifting 100011111100 right 3 times,

Binary Representation becomes 100011111

and Decimal equivalent of 100011111 is 287.

Input: BigInteger = 35000, n = 5

Output: 1093

以下示例程序旨在说明BigInteger的shiftRight(index)方法:

// Program to demonstrate shiftRight()

// method of BigInteger

import java.math.*;

public class GFG {

public static void main(String[] args)

{

// Create BigInteger object

BigInteger biginteger = new BigInteger("2300");

// Create a int i for Shift Distance

int i = 3;

// Call shiftRight() method on bigInteger at index i

// store the return value as BigInteger

BigInteger changedvalue = biginteger.shiftRight(i);

String result = "After applying shiftRight by Shift Distance " + i +

" on " + biginteger + " New Value is " + changedvalue;

// Print result

System.out.println(result);

}

}

输出:

After applying shiftRight by Shift Distance 3 on 2300 New Value is 287

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值