BigInteger bitCount() Method in Java
Prerequisite: BigInteger Basics
The java.math.BigInteger.bitCount() method returns number of bits in the two’s complement representation of this BigInteger that differ from its sign bit. This method is useful when implementing bit-vector style sets atop BigIntegers.
public int bitCount()
Parameters: The method does not take any parameters.
Return Value: The method is used to return the number of bits in the two’s complement representation of this BigInteger that differ from its sign bit.
Input: value = 2300
Binary signed 2's complement of 2300 = 0000100011111100
Signed bit is 0 because 2300 is positive
so no of 1 in 0000100011111100 is bitCount
So bitCount in 0000100011111100 = 7
Input: value = 5482549
Below program illustrate the bitCount() method of BigInteger.
BigInteger biginteger =
count = biginteger.bitCount();
String result =
"BitCount of "
+ biginteger +
" is "
BitCount of 2300 is 7
Reference : https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#bitCount()
Feeling lost in the vast world of Backend Development? It's time for a change! Join our Java Backend Development - Live Course
and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
- Comprehensive Course
- Expert Guidance for Efficient Learning
- Hands-on Experience with Real-world Projects
- Proven Track Record with 100,000+ Successful Geeks