BigInteger setBit() Method in Java
The java.math.BigInteger.setbit(index) method returns a Big-integer whose value is equivalent to this Big-integer with the designated bit set. The method computes (this | (1<<n)). The bit at index n of binary representation of Big-integer will be set means converted to 1.
public BigInteger setbit(int n)
Parameters: The method takes one parameter n which refers to the index of the bit that is needed to be set.
Return Value: The method returns the BigInteger value after setting the bit position n.
Exceptions: The method might throw an ArithmeticException when n is negative..
Input: value = 2300 index = 1 Output: 2302 Explanation: Binary Representation of 2300 = 100011111100 bit at index 1 is 0 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
Below program illustrate the setBit(index) method of BigInteger:
After applying setBit at index 1 of 2300 New Value is 2302