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.
Syntax:
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.
Examples:
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
Below program illustrate the setBit(index) method of BigInteger:
// 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
Reference: https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#setBit(int)
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Java.math.BigInteger.modInverse() method in Java
- Java.math.BigInteger.probablePrime() method in Java
- BigInteger signum() Method in Java
- BigInteger shiftRight() Method in Java
- Java 8 | BigInteger divideAndRemainder() method with Examples
- BigInteger abs() Method in Java
- BigInteger modPow() Method in Java
- BigInteger or() method in Java
- BigInteger xor() Method in Java
- BigInteger and() Method in Java
- BigInteger not() Method in Java
- BigInteger andNot() Method in Java
- BigInteger bitCount() Method in Java
- BigInteger bitLength() Method in Java
- BigInteger clearBit() Method in Java
- BigInteger flipBit() Method in Java
- BigInteger negate() Method in Java
- BigInteger shiftLeft() Method in Java
- BigInteger testBit() Method in Java
- BigInteger getLowestSetBit() Method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

