Java lang.Long.builtcount() method in Java with Examples


java.lang.Long.bitCount() is a built in function in Java that returns the number of set bits in a binary representation of a number. It accepts a single mandatory parameter number whose number of set bits is returned.

Syntax:

public static long bitCount(long num)
Parameters:
num - the number passed 
Returns:
the number of set bits in the binary representation of the number 

Examples:

Input : 8 
Output : 1
Explanation: Binary representation : 1000 
No of set bits=1 

Input : 1032
Output : 2
Explanation: binary representation = 10000001000
no of set bits = 2

The program below illustrates the java.lang.Long.bitCount() function:

Program 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program that demonstrates the use of
// Long.bitCount() funtion
  
// include lang package
import java.lang.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        long l = 1032;
  
        // prints the binary representation of the number
        System.out.println("binary representation = " + Long.toBinaryString(l));
  
        // prints the number of set bits
        System.out.println("no of set bits = " + Long.bitCount(l));
    }
}

chevron_right


Output:

 binary representation = 10000001000
no of set bits = 2

Program 2: When a negative number is passed in the argument

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program that demonstrates the use of
// Long.bitCount() funtion
// Negative number
  
// include lang package
import java.lang.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        long l = -1032;
  
        // prints the binary representation of the number
        System.out.println("binary representation = " + Long.toBinaryString(l));
  
        // prints the number of set bits
        System.out.println("no of set bits = " + Long.bitCount(l));
    }
}

chevron_right


Output:

binary representation = 1111111111111111111111111111111111111111111111111111101111111000
no of set bits = 60 

Error: The function returns an error if any data-type other than long is passed as an argument.

Program 3: When decimal number is passed as an argument

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program that demonstrates the use of
// Long.bitCount() funtion
// decimal number
  
// include lang package
import java.lang.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // prints the number of set bits
        System.out.println("no of set bits = " + Long.bitCount(11.23));
    }
}

chevron_right


Output:

prog.java:15: error: incompatible types: possible lossy conversion from double to long
        System.out.println("no of set bits = " + Long.bitCount(11.23));

Program 4: When string number is passed as an argument

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program that demonstrates the use of
// Long.bitCount() funtion
// string number
  
// include lang package
import java.lang.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // prints the number of set bits
        System.out.println("no of set bits = " + Long.bitCount("12"));
    }
}

chevron_right


Output:

prog.java:15: error: incompatible types: String cannot be converted to long
        System.out.println("no of set bits = " + Long.bitCount("12")); 


My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

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.