Java.lang Package Classes

Java Long - numberOfTrailingZeros() Method



The java.lang.Long.numberOfTrailingZeros() method returns the number of zero bits following the lowest-order ("rightmost") one-bit in the two's complement binary representation of the specified long value. The method returns 64 if the specified value has no one-bits in its two's complement representation, in other words if it is equal to zero.

Syntax

public static int numberOfTrailingZeros(long i)

Parameters

i Specify the value whose number of trailing zeros is to be computed.

Return Value

Returns the number of zero bits following the lowest-order ("rightmost") one-bit in the two's complement binary representation of the specified long value, or 64 if the value is equal to zero.

Exception

NA.

Example:

In the example below, the java.lang.Long.numberOfTrailingZeros() method returns the number of zero bits following the rightmost one-bit 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 a long value
    long x = 50; 

    //printing value of x, its binary 
    //representation and numberOfTrailingZeros
    System.out.println("The values of x is: " + x);
    System.out.print("The binary representation of x is: ");
    System.out.println(Long.toBinaryString(x));
    System.out.print("The numberOfTrailingZeros is: ");
    System.out.println(Long.numberOfTrailingZeros(x));
  }
}

The output of the above code will be:

The values of x is: 50
The binary representation of x is: 110010
The numberOfTrailingZeros is: 1

❮ Java.lang - Long