VOOZH about

URL: https://www.geeksforgeeks.org/java/integer-numberofleadingzeros-method-in-java-with-example/

⇱ Integer.numberOfLeadingZeros() Method in Java With Example - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Integer.numberOfLeadingZeros() Method in Java With Example

Last Updated : 5 Aug, 2025

The Integer.numberOfLeadingZeros(int value) method returns the number of zero bits before the first (leftmost) 1-bit in the binary representation of the given integer. If the value is 0 (i.e., no 1-bits), it returns 32.

Syntax : 

public static int numberOfLeadingZeros(int arg)

Parameter: This method accepts a single parameter arg, which is the integer value.

Return Value: This method returns the number of zero bits preceding the highest-order set-bit in the two's complement binary representation of the specified int value or 32 if the value is equal to zero.

Program 1: Below programs illustrate the java.lang.Integer.numberOfLeadingZeros() method. with positive number. 


Output
Integral Number = 19
Number of Leading Zeros = 27

Explanation:

  • Consider any integer arg, like int arg = 19;
  • Binary Representation(32-bit) of 19 = 00000000 00000000 00000000 00010011
  • First (leftmost) 1 bit is at position 27 (index starts from 0).
  • number of leading zeros = 32 - 5 = 27

Program 2: For a negative number.


Output
Number = -15
Number of Leading Zeros = 0

Program 3: For a decimal value. 


Output
Number of Leading Zeros = 27

Note:It returns an error message when a decimal value is passed as an argument.  

Program 4: For a string value is passed in argument.   


Output
Number of Leading Zeros = 27

Note:It returns an error message when a string is passed as an argument.

Comment