java bit integer_Java BigInteger setBit()用法及代码示例

java.math.BigInteger.setbit(index)方法返回一个Big-integer,其值与此指定位设置的Big-integer等效。该方法计算(this |(1

用法:

public BigInteger setbit(int n)

参数:该方法采用一个参数n,该参数指的是需要设置的位的索引。

返回值:设置位位置n后,该方法返回BigInteger值。

异常:当n为负数时,该方法可能会引发ArithmeticException。

例子:

Input: value = 2300 index = 1

Output: 2302

Explanation:

Binary Representation of 2300 = 100011111100

bit at index 3 is 1 so set the bit at index 1

Now Binary Representation becomes 100011111110

and Decimal equivalent of 100011111110 is 2302

Input: value = 5482549 index = 1

Output: 5482551

以下示例程序旨在说明BigInteger的setBit(index)方法:

// Program to demonstrate setBit() method of BigInteger

import java.math.*;

public class GFG {

public static void main(String[] args)

{

// Creating BigInteger object

BigInteger biginteger = new BigInteger("2300");

// Creating an integer i for index

int i = 1;

// Calling setBit() method on bigInteger at index i

// store the return BigInteger

BigInteger changedvalue = biginteger.setBit(i);

String result = "After applying setBit at index " +

i + " of " + biginteger+ " New Value is " + changedvalue;

// Displaying the result

System.out.println(result);

}

}

输出:

After applying setBit at index 1 of 2300 New Value is 2302

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值