Java-math-biginteger-setbit

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

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

説明

  • java.math.BigInteger.setBit(int n)*は、指定されたビットが設定されたこのBigIntegerと値が等しいBigIntegerを返します。 計算します(this |(1 << n))。

宣言

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

public BigInteger setBit(int n)

パラメーター

*n* -設定するビットのインデックス。

戻り値

このメソッドは、次の値を持つBigIntegerオブジェクトを返します| (1 << n)。

例外

*ArithmeticException* -nが負の場合。

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

package com.finddevguides;

import java.math.*;

public class BigIntegerDemo {

   public static void main(String[] args) {

     //create 2 BigInteger objects
      BigInteger bi1, bi2;

      bi1 = new BigInteger("7");

     //perform setbit operation on bi1 using index 3
      bi2 = bi1.setBit(3);

      String str = "Setbit operation on " +bi1+ " at index 3 gives " +bi2;

     //print bi2 value
      System.out.println( str );
   }
}

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

Setbit operation on 7 at index 3 gives 15