boolean testBit(int n)

优质
小牛编辑
131浏览
2023-12-01

描述 (Description)

当且仅当设置了指定位时, java.math.BigInteger.testBit(int n)返回true。 它计算(这&(1“”n))!= 0)。

声明 (Declaration)

以下是java.math.BigInteger.testBit()方法的声明。

public boolean testBit(int n)

参数 (Parameters)

n - 要测试的位索引

返回值 (Return Value)

当且仅当设置了此BigInteger的指定位时,此方法才返回true。

异常 (Exception)

ArithmeticException - n为负数

例子 (Example)

以下示例显示了math.BigInteger.testBit()方法的用法。

package cn.xnip;
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