當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。