当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java BigInteger flipBit()用法及代码示例


先决条件:BigInteger基础
java.math.BigInteger.flipBit(index)方法返回一个BigInteger,该值用于翻转BigInteger中的特定位位置。此方法计算(bigInteger ^(1

用法:

public BigInteger flipBit(int index)

参数:该方法接受一个整数类型的参数索引,表示要翻转的位的位置。


返回值:该方法在位置索引处翻转bigInteger后返回其值。

抛出:当index的值为负时,该方法抛出ArithmeticException。

例子:

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

以下示例程序旨在说明BigInteger的flipBit(index)方法。

/* 
*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(int)



相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 BigInteger flipBit() Method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。