Java BigInteger shiftLeft()方法

Java BigInteger shiftLeft()方法

java.math.BigInteger.shiftLeft(int n) 方法返回一个BigInteger,其值为(this << n)。ShiftLeft()方法将把一个数字的二进制表示法中的每个数字向左移动n倍,移动方向的最后一位被替换为0。

语法

public BigInteger shiftLeft(int n)

参数: 该方法需要一个整数类型的参数n,它指的是移位距离,单位为比特。

返回值: 该方法在将比特向左移位n次后返回BigInteger。

异常: 如果移位距离是Integer.MIN_VALUE,该方法可能会抛出一个ArithmeticException。

例子

输入: value = 2300, shift distance = 3
输出: 18400
解释:
2300=100011111100的二进制表示法
移位距离=3
在将100011111100左移3次后,那么
二进制表示法成为100011111100000
而十进制相当于100011111100000是18400。
另一种表达方式可以是2300*(2^3)=18400

输入: value = 35000, index = 5
输出: 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

**参考资料: **https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#shiftLeft(int)

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程