I have an assignment in which I need to create a function that tells you how many 1’s are in the binary notation of a integer. I did this already by creating my own algorithm. The second step is to use java.math.BigInteger.bitCount() to accomplish the same thing. I looked this up in the Java API but can someone put this into English and explain how it’s relevant to finding the number of 1’s in the binary notation of an integer, and perhaps also an example. I tried googling but found nothing but the following definition.
public int bitCount()
Description:
Returns the number of bits in the two’s complement representation of this number that differ from its sign bit. This method is useful when implementing bit-vector style sets atop BigIntegers.
Modified code for new comment:
Obviously, from reading the javadoc you posted, this may or may not conform to what you or your professor expect for negative numbers. This will return 0 “1” bits for negative 1, for example, since all the bits are the same as the sign bit. But it should work for all positive values.