java.math.BigInteger.shiftLeft(int n)方法返回一个BigInteger,其值为(this
用法:
public BigInteger shiftLeft(int n)
参数:该方法采用整数类型的一个参数n,它表示移位距离(以位为单位)。
返回值:该方法将位左移n次后返回BigInteger。
异常:如果移位距离为Integer.MIN_VALUE,则该方法可能会引发ArithmeticException。
例子:
Input: value = 2300, shift distance = 3
Output: 18400
Explanation:
Binary Representation of 2300 = 100011111100
shift distance = 3
after shifting 100011111100 left 3 times then
Binary Representation becomes 100011111100000
and Decimal equivalent of 100011111100000 is 18400.
Another way of expressing the same can be 2300*(2^3)=18400
Input: value = 35000, index = 5
Output: 1120000
以下示例程序旨在说明BigInteger的shiftLeft(index)方法。
// Program to demonstrate shiftLeft() method of BigInteger
import java.math.*;
public class GFG {
public static void main(String[] args)
{
// Creating BigInteger object
BigInteger biginteger = new BigInteger("2300");
// Creating a int i for Shift Distance
int i = 3;
// Call shiftLeft() method on bigInteger at index i
// store the return value as BigInteger
BigInteger changedvalue = biginteger.shiftLeft(i);
String result = "After applying ShiftLeft by Shift Distance " + i
+ " on " + biginteger + " New Value is " + changedvalue;
// Printing result
System.out.println(result);
}
}
输出:
After applying ShiftLeft by Shift Distance 3 on 2300 New Value is 18400
本文详细介绍了Java中BigInteger的shiftLeft()方法,该方法用于将一个BigInteger的位向左移动指定次数。通过示例展示了如何使用此方法,例如将数值2300左移3位得到18400。同时,文中还包含了一个简单的Java程序来演示shiftLeft()方法的使用。
496

被折叠的 条评论
为什么被折叠?



