Java-math-biginteger-flipbit

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

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

説明

  • java.math.BigInteger.flipBit(int n)*は、指定されたビットが反転されたこのBigIntegerと同等の値を持つBigIntegerを返します。 計算します(this ^(1 << n))。

宣言

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

public BigInteger flipBit(int n)

パラメーター

*n* -反転するビットのインデックス。

戻り値

このメソッドは、値がthis ^(1 << n)であるBigIntegerオブジェクトを返します。

例外

*ArithmeticException* -nは負です。

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

package com.finddevguides;

import java.math.*;

public class BigIntegerDemo {

   public static void main(String[] args) {

     //create 2 BigInteger objects
      BigInteger bi1, bi2;

     //assign value to bi1
      bi1 = new BigInteger("8");//1000

     //perform flipbit operation on bi1 with index 1
      bi2 = bi1.flipBit(1);

      String str = "Flipbit operation on " +bi1+ " at index 1 gives " +bi2;

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

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

Flipbit operation on 8 at index 1 gives 10