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

本文详细介绍了Java中BigInteger的shiftLeft()方法,该方法用于将一个BigInteger的位向左移动指定次数。通过示例展示了如何使用此方法,例如将数值2300左移3位得到18400。同时,文中还包含了一个简单的Java程序来演示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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值