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.
Syntax:
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.
Examples:
Input: value = 2300 Output: 8 Explanation: Binary signed 2's complement of 2300 = 0000100011111100 Singned bit is 0 because 2300 is positive so no of 1 in 0000100011111100 is bitCount So bitCount in 0000100011111100 = 8 Input: value = 5482549 Output: 11
Below program illustrate the bitCount() method of BigInteger.
/* *Program Demonstrate bitCount() method of BigInteger */import java.math.*; public class GFG { public static void main(String[] args) { // Creates BigInteger objects BigInteger biginteger = new BigInteger("2300"); // Calling bitCount() method on bigInteger int count = biginteger.bitCount(); String result = "BitCount of " + biginteger + " is " + count; // Print result System.out.println(result); } } |
BitCount of 2300 is 7
Reference : https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#bitCount()
Recommended Posts:
- Java Integer bitCount() method
- BigInteger or() method in Java
- BigInteger not() Method in Java
- BigInteger abs() Method in Java
- BigInteger xor() Method in Java
- BigInteger and() Method in Java
- BigInteger mod() Method in Java
- BigInteger pow() Method in Java
- BigInteger longValue() Method in Java
- BigInteger intValue() Method in Java
- BigInteger hashCode() Method in Java
- BigInteger compareTo() Method in Java
- BigInteger equals() Method in Java
- BigInteger floatValue() Method in Java
- BigInteger remainder() Method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



