如何计算 BigInteger 中 1 位的数量?

How to calculate number of 1 bit's in a BigInteger ?

我正在解决一个问题,我需要知道 BigInteger 中 1 位的数量。

提前致谢。

您可以在 BigInteger 上使用 .bitCount()。除非你需要手动解决它,在这种情况下你可以使用正常的Java位运算。

用 0 做 x-or。这样你就可以在原始输入中有 1 的地方得到设置为 1 的位。然后你可以计算输出中设置的位数。