Java中的BigInteger testBit()方法

先决条件: 大整数基础 这个 JAVA数学。大整数。测试位( 指数 ) 当且仅当指定位已设置时,方法返回true。这种方法计算 (这个&(1< .

null

语法:

public boolean testBit(int n)

参数: 该方法采用一个参数 N 整数类型,指需要测试的位的索引。

返回值: 当且仅当指定的位被设置时,该方法返回true,否则将返回false。

例外情况: 该方法将抛出一个 算术异常 当n为负时。 例如:

Input: BigInteger = 2300, n = 4
Output: true
Explanation:
Binary Representation of 2300 = 100011111100
bit at index 4 is 1 so set it means bit is set
so method will return true

Input: BigInteger = 5482549 , n = 1
Output: false

下面的程序演示了BigInteger的testBit()方法。

// Program to demonstrate the testBit()
// method of BigInteger
import java.math.*;
public class GFG {
public static void main(String[] args)
{
// Creating a BigInteger object
BigInteger biginteger = new BigInteger( "2300" );
// Creating an int i for index
int i = 3 ;
boolean flag = biginteger.testBit(i);
String result = "The bit at index " + i + " of " +
biginteger + " is set = " + flag;
// Displaying the result
System.out.println(result);
}
}


输出:

The bit at index 3 of 2300 is set = true

参考: https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#testBit(国际)

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享