Java-math-biginteger-testbit

提供:Dev Guides
移動先:案内検索

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.finddevguides;

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