Java中的BigInteger setBit()方法

这个 JAVA数学大整数。挫折(指数) 方法返回一个大整数,其值等于指定位集的这个大整数。该方法计算(这个|(1<

null

语法:

public BigInteger setbit(int n)

参数: 该方法采用一个参数 N 它是指需要设置的位的索引。 返回值: 该方法在设置位位置n后返回BigInteger值。 例外情况: 该方法可能会引发 算术异常 当n为负时。。

例子 :

Input: value = 2300 index = 1Output: 2302Explanation:Binary Representation of 2300 = 100011111100bit at index 1 is 0 so set the bit at index 1 Now Binary Representation becomes 100011111110and Decimal equivalent of 100011111110 is 2302Input: value = 5482549 index = 1Output: 5482551

下面的程序演示了BigInteger的setBit(index)方法:

JAVA

// Program to demonstrate setBit() method of BigInteger
import java.math.*;
public class GFG {
public static void main(String[] args)
{
// Creating BigInteger object
BigInteger biginteger = new BigInteger(" 2300 ");
// Creating an integer i for index
int i = 1 ;
// Calling setBit() method on bigInteger at index i
// store the return BigInteger
BigInteger changedvalue = biginteger.setBit(i);
String result = "After applying setBit at index " +
i + " of " + biginteger+ " New Value is " + changedvalue;
// Displaying the result
System.out.println(result);
}
}


输出:

After applying setBit at index 1 of 2300 New Value is 2302

参考: https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#setBit(国际)

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享