Java-math-biginteger-getlowestsetbit

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

Java.math.BigInteger.getLowestSetBit()メソッド

説明

  • java.math.BigInteger.getLowestSetBit()*は、このBigIntegerの右端(最下位)の1ビットのインデックス(右端の1ビットの右側のゼロビットの数)を返します。 このBigIntegerに1ビットも含まれていない場合、-1を返します。 計算します(this == 0? -1:log2(this&-this))。

宣言

以下は、* java.math.BigInteger.getLowestSetBit()*メソッドの宣言です。

public int getLowestSetBit()

パラメーター

NA

戻り値

このメソッドは、このBigIntegerの右端の1ビットのインデックスを返します。

例外

NA

次の例は、math.BigInteger.getLowestSetBit()メソッドの使用方法を示しています。

package com.finddevguides;

import java.math.*;

public class BigIntegerDemo {

   public static void main(String[] args) {

     //create 2 BigInteger objects
      BigInteger bi1, bi2;

     //create 2 int objects
      int i1, i2;

     //assign values to bi1, bi2
      bi1 = new BigInteger("8");//1000
      bi2 = new BigInteger("7");//0111

     //perform getLowestSetBit on bi1, bi2
      i1 = bi1.getLowestSetBit();
      i2 = bi2.getLowestSetBit();

      String str1 = "Index of rightmost one bit in " +bi1+ " is " +i1;
      String str2 = "Index of rightmost one bit in " +bi2+ " is " +i2;

     //print i1, i2 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

上記のプログラムをコンパイルして実行すると、次の結果が生成されます-

Index of rightmost one bit in 8 is 3
Index of rightmost one bit in 7 is 0