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 物件,其值是 this | (1<<n)。

異常

算術異常 − 如果 n 為負數。

示例

以下示例顯示了 math.BigInteger.setBit() 方法的用法。

package com.tutorialspoint;

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
java_math_biginteger.htm
廣告
© . All rights reserved.