当前位置: 首页>>代码示例>>用法及示例精选>>正文


Java BigInteger shiftLeft()用法及代码示例

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

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



相关用法

注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 BigInteger shiftLeft() Method in Java。非经特殊声明,原始代码版权归原作者所有,本译文的传播和使用请遵循“署名-相同方式共享 4.0 国际 (CC BY-SA 4.0)”协议。