Facebook Page Twitter Page LinkedIn Page
× Java.lang Package Classes


The java.lang.Long.bitCount() method is used to return the number of one-bits in the two's complement binary representation of the specified long value. This function is sometimes referred to as the population count.

Syntax

public static int bitCount(long i)

Parameters

i Specify the value whose bits are to be counted.

Return Value

Returns the number of one-bits in the two's complement binary representation of the specified long value.

Exception

NA.

Example:

In the below example, the java.lang.Long.bitCount() method is used to return the number of one-bits in the two's complement binary representation of the given long value.

import java.lang.*;

public class MyClass {
  public static void main(String[] args) {
    
    //creating long value
    long x = 135;

    //printing x
    System.out.println("The x is = " + x); 

    //printing the binary representation of x
    System.out.println("The x in binary is = " + Long.toBinaryString(x)); 

    //printing the number of one-bits in x
    System.out.println("Number of one bits in x = " + Long.bitCount(x)); 
  }
}

The output of the above code will be:

The x is = 135
The x in binary is = 10000111
Number of one bits in x = 4

❮ Java.lang - Long