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 Next Article Java Integer bitCount() Method N Niraj_Pandey Follow Improve Article Tags : Java Java-lang package Java-Functions Java-Integer Practice Tags : Java Similar Reads Java Integer byteValue() Method The byteValue() method of Integer class of java.lang package converts the given Integer into a byte after a narrowing primitive conversion and returns it (value of integer object as a byte). Also, remember this method does override byteValue() method of the Number class. The package view is as follo 2 min read 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() Para 1 min read BigInteger and() Method in Java The java.math.BigInteger.and(BigInteger val) method returns a BigInteger whose value is bitwise-AND of two BigIntegers. This method returns a negative number if both of the BigIntegers are negative. The and() method applies bitwise-AND operation upon the current bigInteger and bigInteger passed as p 2 min read BigInteger not() Method in Java The java.math.BigInteger.not() method is used to find the bitwise-NOT of a BigInteger. This method returns a negative value if and only if this BigInteger is non-negative. The BigInteger.not() method apply bitwise Not operation upon the current bigInteger. Syntax: public BigInteger not() Parameters: 1 min read Java Program to Count set bits in an integer Write an efficient program to count number of 1s in binary representation of an integer. Examples :Input : n = 6Output : 2Binary representation of 6 is 110 and has 2 set bitsInput : n = 13Output : 3Binary representation of 11 is 1101 and has 3 set bits1. Simple Method Loop through all bits in an int 2 min read Integer lowestOneBit() Method in Java The Integer.lowestOneBit() method of java.lang is an inbuilt function that returns an int value with at most a single one-bit, in the position of the lowest-order (ie.rightmost) one-bit in the specified int value. This method will return zero if the specified value has no one-bits in its two's compl 3 min read BigInteger setBit() Method in Java The java.math.BigInteger.setbit(index) method returns a Big-integer whose value is equivalent to this Big-integer with the designated bit set. The method computes (this | (1<<n)). The bit at index n of binary representation of Big-integer will be set means converted to 1. Syntax: public BigInt 2 min read BitSet isEmpty() method in Java BitSet is a class defined in the java.util package. It creates an array of bits represented by boolean values. Prerequisite : Java BitSet | Set 1 Bitset.isEmpty() This method is used to check if there are any bits that are set to true, in the specified BitSet. This method returns true if this BitSet 2 min read Integer reverseBytes() Method in Java The java.lang.Integer.reverseBytes(int a) is a built-in method which returns the value obtained by reversing the order of the bytes in the two's complement representation of the specified int value. Syntax : public static int reverseBytes(int a) Parameter: The method takes one parameter a of integer 3 min read BigInteger testBit() Method in Java prerequisite : BigInteger Basics The java.math.BigInteger.testBit(index) method returns true if and only if the designated bit is set. This method Computes (this & (1<<n)) != 0). Syntax: public boolean testBit(int n) Parameter: The method takes one parameter n of integer type which refers 2 min read Like