java.lang.Integer numberOfLeadingZeros(int i)

Description :

This java tutorial shows how to use the numberOfLeadingZeros() method of Integer class under java.lang package. This method returns an int which corresponds to the number of zeroes on the left most part of the 2’s complement equivalent of int method argument.

Method Syntax :

public static int numberOfLeadingZeros(int i)

Parameter Input :

DataType Parameter Description
int i the int value that we want to get the leading zeroes or in other words the highest order bit on its two's complement.

Method Returns :

The numberOfLeadingZeros(int i) method simply returns the number of zero bits preceding the highest-order (“leftmost”) one-bit in the two’s complement binary representation of the specified int value, or 32 if the value is equal to zero.

Compatibility Version :

Requires Java 1.5 and up

Exception :

N/A

Java Code Example :

This java example source code demonstrates the use of numberOfLeadingZeros() method of Integer class. Basically we only presented on how to use this method.

package com.javatutorialhq.java.examples;

import static java.lang.System.*;

/*
 * This example source code demonstrates 
 * the use of numberOfLeadingZeros method of Integer class
 */

public class IntegerNumberOfLeadingZeros {

	public static void main(String[] args) {
		int value = 178;
		// Get the binary equivalent
		out.println("Binary equivalent:"+Integer.toBinaryString(value));
		// get the number of leading zeros
		out.println("Number of Leading Zeros:"+Integer.numberOfLeadingZeros(value));

	}

}

Sample Output :

Running the numberOfLeadingZeros() method example source code of Integer class will give you the following output

Binary equivalent:10110010
Number of Leading Zeros:24

Exception Scenario :

N/A

Similar Method :

  • N/A

Suggested Reading List :

References :