java.math.BigInteger.setbit(index)方法返回一個Big-integer,其值與此指定位設置的Big-integer等效。該方法計算(this |(1
用法:
public BigInteger setbit(int n)
參數:該方法采用一個參數n,該參數指的是需要設置的位的索引。
返回值:設置位位置n後,該方法返回BigInteger值。
異常:當n為負數時,該方法可能會引發ArithmeticException。
例子:
Input: value = 2300 index = 1 Output: 2302 Explanation: Binary Representation of 2300 = 100011111100 bit at index 3 is 1 so set the bit at index 1 Now Binary Representation becomes 100011111110 and Decimal equivalent of 100011111110 is 2302 Input: value = 5482549 index = 1 Output: 5482551
以下示例程序旨在說明BigInteger的setBit(index)方法:
// 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(int)
相關用法
- Java BigInteger mod()用法及代碼示例
- Java BigInteger pow()用法及代碼示例
- Java BigInteger abs()用法及代碼示例
- Java BigInteger not()用法及代碼示例
- Java BigInteger or()用法及代碼示例
- Java BigInteger xor()用法及代碼示例
- Java BigInteger and()用法及代碼示例
- Java BigInteger compareTo()用法及代碼示例
- Java BigInteger toString()用法及代碼示例
- Java BigInteger hashCode()用法及代碼示例
- Java BigInteger testBit()用法及代碼示例
- Java BigInteger getLowestSetBit()用法及代碼示例
- Java BigInteger valueOf()用法及代碼示例
- Java BigInteger sqrt()用法及代碼示例
- Java BigInteger equals()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 BigInteger setBit() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。