Java中的BigInteger flipBit()方法

先决条件: 大整数基础 这个 JAVA数学大整数。flipBit(索引) 方法返回一个BigInteger,用于翻转BigInteger中的特定位位置。此方法计算(bigInteger^(1< 语法:

null
public BigInteger flipBit(int index)

参数: 该方法接受一个参数 指数 整数类型,指要翻转的位的位置。 返回值: 该方法在位置翻转位后返回bigInteger 指数 . 抛出: 该方法抛出了一个 算术异常 当索引值为负值时。 例如:

Input: value = 2300 , index = 1Output: 2302Explanation:Binary Representation of 2300 = 100011111100bit at index 1 is 0 so flip the bit at index 1 and it becomes 1. Now Binary Representation becomes 100011111110and Decimal equivalent of 100011111110 is 2302Input: value = 5482549 , index = 5Output: 5482517

下面的程序演示BigInteger的flipBit(索引)方法。

JAVA

/*
*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

参考: https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#clearBit(国际)

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享