Java Integer bitCount() Method Last Updated : 21 Jan, 2025 Comments Improve Suggest changes 12 Likes Like Report 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 countedReturn: 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. java // Java program to demonstrate working // of java.lang.Integer.bitCount() method import java.lang.Integer; class Gfg { // driver code public static void main(String args[]) { int a = 10; // Convert integer number to binary format System.out.println(Integer.toBinaryString(a)); // to print number of 1's in the number a System.out.println(Integer.bitCount(a)); } } Output1010 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: Java import java.io.*; class GFG { public static void main(String[] args) { int num1 = 10; // binary representation: 1010 int num2 = -10; // binary representation: // 11111111111111111111111111110110 int result1 = Integer.bitCount(num1); int result2 = Integer.bitCount(num2); System.out.println("Number of one-bits in num1: " + result1); System.out.println("Number of one-bits in num2: " + result2); } } OutputNumber 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. Create Quiz Comment N Niraj_Pandey Follow 12 Improve N Niraj_Pandey Follow 12 Improve Article Tags : Java Java-lang package Java-Functions Java-Integer Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References7 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers1 min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like