Java.math.BigInteger.testBit() 方法



描述

java.math.BigInteger.testBit(int n) 僅在指定位被設定時返回 true。(該位為 this & (1<<n) != 0 )。

宣告

以下是 java.math.BigInteger.testBit() 方法的宣告。

public boolean testBit(int n)

引數

n - 要測試的位索引

返回值

僅當此 BigInteger 的指定位被設定時,此方法返回 true。

異常

ArithmeticException - n 為負數

示例

以下示例演示瞭如何使用 math.BigInteger.testBit() 方法。

package com.tutorialspoint;

import java.math.*;

public class BigIntegerDemo {

   public static void main(String[] args) {

      // create a BigInteger object
      BigInteger bi;

      // create 2 boolean objects
      Boolean b1, b2;

      bi = new BigInteger("10"); 

      // perform testbit on bi at index 2 and 3
      b1 = bi.testBit(2);
      b2 = bi.testBit(3);

      String str1 = "Test Bit on " + bi + " at index 2 returns " +b1;
      String str2 = "Test Bit on " + bi + " at index 3 returns " +b2;

      // print b1, b2 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

讓我們編譯並執行上述程式,它將產生以下結果 -

Test Bit on 10 at index 2 returns false
Test Bit on 10 at index 3 returns true
java_math_biginteger.htm
廣告
© . All rights reserved.