Java Integer bitCount() Method Last Updated : 21 Jan, 2025 Comments Improve Suggest changes Like Article 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. Comment More infoAdvertise with us N Niraj_Pandey Follow Improve Article Tags : Java Java-lang package Java-Functions Java-Integer Practice Tags : Java Explore BasicsIntroduction to Java4 min readJava Programming Basics9 min readJava Methods7 min readAccess Modifiers in Java6 min readArrays in Java9 min readJava Strings8 min readRegular Expressions in Java7 min readOOPs & InterfacesClasses and Objects in Java10 min readJava Constructors10 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface11 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java6 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling8 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 Tutorial15+ min readSynchronization in Java10 min readFile Handling in Java5 min readJava Method References9 min readJava 8 Stream Tutorial15+ min readJava Networking15+ min readJDBC Tutorial12 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples8 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions7 min readJava Quiz | Level Up Your Java Skills1 min readTop 50 Java Project Ideas For Beginners and Advanced [Update 2025]15+ min read Like