設定 Java 中 BigInteger 的位


setBit() 方法用於在 Java 中返回一個 BigInteger,其值等效於這個設定了指定位的 BigInteger。

示例

 現場演示

import java.math.*;
public class BigIntegerDemo {
   public static void main(String[] args) {
      BigInteger one, two;
      one = new BigInteger("7");
      two = one.setBit(3);
      System.out.println("Result: " +two);
   }
}

輸出

Result: 15

讓我們看另一個示例。

示例

 現場演示

import java.math.*;
public class Demo {
   public static void main(String[] args) {
      BigInteger bi1, bi2;
      bi1 = new BigInteger("9");
      // setbit operation using index 3
      bi2 = bi1.setBit(3);
      String res = "Setbit operation on " +bi1+ " at index 3 gives " +bi2;
      System.out.println(res);
   }
}

輸出

Setbit operation on 9 at index 3 gives 9

更新於:2020 年 6 月 25 日

148 次瀏覽

開啟您的職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.