當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java BigInteger clearBit()用法及代碼示例


先決條件:BigInteger基礎

clearBit()方法返回一個BigInteger,用於清除BigInteger中的特定位位置。 BigInteger二進製表示的索引n處的位將被清除,表示轉換為零。從數學上講,我們可以說它用於計算&〜(1

用法:


public BigInteger clearBit(int n)

參數:該方法采用一個參數n,該參數指需要清除的位的索引。

返回值:清除位位置n後,該方法將返回BigInteger。

拋出:當n為負數時,該方法可能拋出ArithmeticException。

例子:

Input: value = 2300, index = 3
Output: 2292
Explanation:
Binary Representation of 2300 = 100011111100
bit at index 3 is 1 so clear the bit at index 3 
Now Binary Representation becomes 100011110100
and Decimal equivalent of 100011110100 is 2292

Input: value = 5482549, index = 0
Output: 5482548

下麵的程序演示了BigInteger()的clearBit(index)方法。

/* 
*Program Demonstrate clearBit() 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 = 0; 
  
        // Call clearBit() method on bigInteger at index i 
        // store the return BigInteger 
        BigInteger changedvalue = biginteger.clearBit(i); 
  
        String result = "After applying clearbit at index " +  
        i + " of " + biginteger+" New Value is " + changedvalue; 
  
        // Print result 
        System.out.println(result); 
    } 
}
輸出:
After applying clearbit at index 0 of 5482549 New Value is 5482548

參考:https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#clearBit(int)



相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 BigInteger clearBit() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。