BigInteger类setBit()方法 (BigInteger Class setBit() method)
setBit() method is available in java.math package.
setBit()方法在java.math包中可用。
setBit() method is used to set the bit value "1" at the given position in this BigInteger.
setBit()方法用于在此BigInteger中的给定位置设置位值“ 1”。
setBit() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
setBit()方法是一种非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
setBit() method may throw an exception at the time of setting a bit.
setBit()方法在设置位时可能会引发异常。
ArithmeticException: This exception may throw when the given parameter value is less than 0.
ArithmeticException :当给定参数值小于0时,可能引发此异常。
Syntax:
句法:
public BigInteger setBit(int pos);
Parameter(s):
参数:
int pos – represents the position of bit set in this BigInteger.
int pos –表示此BigInteger中设置的位的位置。
Return value:
返回值:
The return type of this method is BigInteger, it returns BigInteger that holds the value [(this BigInteger) or (1<< pos)].
此方法的返回类型为BigInteger ,它返回保存值[(this BigInteger)或(1 << pos)]的BigInteger。
Example:
例:
// Java program to demonstrate the example
// of BigInteger setBit(int pos) method of BigInteger
import java.math.*;
public class SetBitOfBI {
public static void main(String args[]) {
// Initialize a variable str
String str = "10";
// Initialize a BigInteger object
BigInteger b_int = new BigInteger(str);
// Display b_int
System.out.println("b_int: " + b_int);
// Display Binary representation of str
System.out.println("Binary Representation of 10: 1010 ");
System.out.println();
// Here, this method setBit(int) method is used
// to set the bit value from "0" to 1" at the
// given indices in this BigInteger
// b_int and the binary representation of b_int
// is 1010 so the bit "1" is set at the given
// indices (0) so the new binary representation
// is 1011 i.e. 11
BigInteger set_bit = b_int.setBit(0);
System.out.println("b_int.setBit(0): " + set_bit);
System.out.println("Binary Representation of 11: 1011 ");
}
}
Output
输出量
b_int: 10
Binary Representation of 10: 1010
b_int.setBit(0): 11
Binary Representation of 11: 1011
翻译自: https://www.includehelp.com/java/biginteger-setbit-method-with-example.aspx