BigInteger flipBit() Method in Java
Prerequisite: BigInteger Basics
The java.math.BigInteger.flipBit(index) method returns a BigInteger which is used to flip a particular bit position in a BigInteger. This method Computes (bigInteger ^ (1<<n)). The bit at index n of binary representaion of the bigInteger will be fliped. That is, if the bit position is 0 it will be converted to 1 and vice versa.
Syntax:
public BigInteger flipBit(int index)
Parameter:The method accepts one parameter index of integer type and refers to the position of the of bit to be flipped.
Return Value: The method returns the bigInteger after flipping its bit at position index.
Throws: The method throws an ArithmeticException when the value of index is negative.
Examples:
Input: value = 2300 , index = 1 Output: 2302 Explanation: Binary Representaion of 2300 = 100011111100 bit at index 1 is 0 so flip the bit at index 1 and it becomes 1. Now Binary Representation becomes 100011111110 and Decimal equivalent of 100011111110 is 2302 Input: value = 5482549 , index = 5 Output: 5482517
Below program illustrate flipBit(index) method of BigInteger.
/* *Program Demonstrate flipBit() method of BigInteger */import java.math.*; public class GFG { public static void main(String[] args) { // Creating BigInteger object BigInteger biginteger = new BigInteger("5482549"); // Creating an int i for index int i = 5; // Call flipBit() method on bigInteger at index i // store the return BigInteger BigInteger changedvalue = biginteger.flipBit(i); String result = "After applying flipBit at index " + i + " of " + biginteger+ " New Value is " + changedvalue; // Print result System.out.println(result); } } |
After applying flipBit at index 5 of 5482549 New Value is 5482517
Reference:https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#clearBit(int)
Recommended Posts:
- BigInteger pow() Method in Java
- BigInteger abs() Method in Java
- BigInteger mod() Method in Java
- BigInteger not() Method in Java
- BigInteger and() Method in Java
- BigInteger or() method in Java
- BigInteger xor() Method in Java
- BigInteger setBit() Method in Java
- BigInteger signum() Method in Java
- BigInteger gcd() Method in Java with Examples
- BigInteger shiftRight() Method in Java
- BigInteger shiftLeft() Method in Java
- BigInteger toString() 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.



