VOOZH about

URL: https://www.geeksforgeeks.org/java/java-integer-bitcount-method/

⇱ Java Integer bitCount() Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java Integer bitCount() Method

Last Updated : 21 Jan, 2025

The bitCount() method of Integer class of java.lang package returns the count of set bits in a positive number. For negative numbers, it returns the count of set bits in its 2s complement form. This function is sometimes referred to as the population count

Syntax:

public static int bitCount(int n)

  • Parameter: n: the value whose bits are to be counted
  • Return: This method returns the count of set bits in a positive number. For negative numbers, it returns the count of set bits in its 2's complement form.

Example 1: To show the working of java.lang.Integer.bitCount() method. 


Output
1010
2
  • Time Complexity: O(1), because the number of bits is fixed (32 bits for int).
  • Auxiliary Space: O(1), as no extra space proportional to the input is used.

Example 2:


Output
Number of one-bits in num1: 2
Number of one-bits in num2: 30
  • Time Complexity: O(1), because the number of bits is fixed (32 bits for int).
  • Auxiliary Space: O(1), as no extra space proportional to the input is used.
Comment