如何在设置所有位的情况下创建256位的BigInteger [英] How to create BigInteger of 256 bits with all bits set

查看:179
本文介绍了如何在设置所有位的情况下创建256位的BigInteger的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建256位全部设置的BigInteger?我已经尝试过以下内容:

How can I create a BigInteger with 256 bits that are all set ? I've already tried the following:

BigInteger.valueOf(0xFFFFFFFFFFFFFFFFL)

但它没有给我预期的结果:

But it doesn't give me the desired result:

int bitCount = b.bitCount();// 0
int bitLength = b.bitLength();// 0

我基本上需要的是一个包含256位的数字,这些数字都已设置。

What I basically need is a number containing 256 bits which are all set.

Tnx!

推荐答案

试试这个:

BigInteger.ONE.shiftLeft(256).subtract(BigInteger.ONE)

它先计算2 ^ 256,这是二进制的一个然后是256个零,然后减去一个只留下256个​​的零。

It calculates first 2^256, which is in binary a one followed by 256 zeroes, and then subtracts one which leaves just 256 ones.

这篇关于如何在设置所有位的情况下创建256位的BigInteger的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆