- Java.math 软件包附加组件
- Java.math - 枚举
- Java.math - 讨论
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
广告