先决条件: 大整数基础 这个 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