New Tutorials:   NUMPY    TKINTER    KOTLIN    JAVASCRIPT    SASS/SCSS    PL/SQL    Matplotlib    C++ Programs

Java Long bitCount() Method

Java bitCount() method belongs to the Long class. This method is used to return the total number of one-bit in two's complement binary form of the specified long value.

Syntax:

public static int bitCount(long i)

Parameters:

The only parameter that is passed is the long i, of which total number of one bit in its two's complement binary form will be returned.

Returns:

This method returns the total number of one-bit in the two's complement binary form of the long value, hence its return type is int.

Example 1:

Here, we are entering a long number 365. The Long.toBinaryString() method will convert the number into its equivalent binary string (101101101 for the integer 365). Then the total number of one bit in the binary representation will be returned.

import java.lang.Long;

public class StudyTonight 
{
    public static void main(String[] args)
    {
        long i = 365;
        long j = -365;
        System.out.println("Actual Number is " + i);
    
        // converting the number to its equivalent binary form with base 2 
        System.out.println("Binary conversion of" + i + " = " + Long.toBinaryString(i));

        //returning the number of one-bits 
        System.out.println("Number of one bits = " + Long.bitCount(i)); 
        System.out.println("*******************");
        System.out.println("Actual Number is " + j);
        System.out.println("Binary conversion of" + j + " = " + Long.toBinaryString(j));
        System.out.println("Number of one bits = " + Long.bitCount(j)); 
     }
}


Actual Number is 365
Binary conversion of 365 = 101101101
Number of one bits = 6
*******************
Actual Number is -365
Binary conversion of-365 = 1111111111111111111111111111111111111111111111111111111010010011
Number of one bits = 59

Example 2:

Here, we are taking an array of long values. The Long.toBinaryString() method will convert the numbers in the array into a binary String.

import java.lang.Long;

public class StudyTonight
{
    public static void main(String args[]) 
    {
        long [] set = {72, 78, 56, 89,80, 66};
        try
        {
            for(int i=0; i<6; i++)
            {
                //Converting the actual number to binary String
                String b = Long.toBinaryString(set[i]); 
                //Converting the binary String to binary long of base 2
                long c = Long.parseLong(b,2); 
                //Counting the set bits
                int d = Long.bitCount(c); 
                
                System.out.println("Actual number is "+ set[i] +" binary sequence : "   + b + ",number of set bits are : " + d);
             }
        }
        catch(Exception e)
        {
             System.out.println("Exception caught: " + e);
        }

    }
}


Actual number is 72 binary sequence : 1001000,number of set bits are : 2
Actual number is 78 binary sequence : 1001110,number of set bits are : 4
Actual number is 56 binary sequence : 111000,number of set bits are : 3
Actual number is 89 binary sequence : 1011001,number of set bits are : 4
Actual number is 80 binary sequence : 1010000,number of set bits are : 2
Actual number is 66 binary sequence : 1000010,number of set bits are : 2

Example 3:

Here is a general example where the user can enter the number of his choice and get the desired output. The try and catch block (Java Try and Catch) is used for handling any exception taking place during the execution of the program. (For example, the user enters a String, etc. in place of the long value).

import java.lang.Long;
import java.util.*; 

public class StudyTonight 
{
    public static void main(String[] args)
    {
        System.out.println("Enter number");
        Scanner sc = new Scanner(System.in);
        try
        {
            long i = sc.nextLong();
            System.out.println("Actual Number is " + i);
    
            // converting the number to its equivalent binary form with base 2 
            System.out.println("Binary conversion of " +i+ " = " + Long.toBinaryString(i));

            //returning the number of one-bits 
            System.out.println("Number of one bits = " + Long.bitCount(i)); 
        }
        catch( Exception e)
        {
            System.out.println("Error!!"); 
        }
    }
}


Enter number
895687
Actual Number is 895687
Binary conversion of 895687 = 11011010101011000111
Number of one bits = 12
***************************************************************
Enter number
-533
Actual Number is -533
Binary conversion of -533 = 1111111111111111111111111111111111111111111111111111110111101011
Number of one bits = 61
*************************************************
Enter number
0x4567
Error!!

Live Example:

Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.