Java Integer.numberOfLeadingZeros(int i) example

Java Integer.numberOfLeadingZeros(int i) example

By Yashwant Chavan, Views 6, Date 21-Apr-2018

In this java tutorial, You will learn how to use numberOfLeadingZeros(int i) method of class Integer. This method 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. Returns 32 if the specified value has no one-bits in its two's complement representation, in other words if it is equal to zero.

tags Java

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.

Syntax

public static int numberOfLeadingZeros(int i)

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.

Java Integer numberOfLeadingZeros(int i) example

  • Assign two integer values.
  • Integer.lowestOneBit(firstValue);It will return the number of leading zeros in firstValue.
  • Integer.lowestOneBit(secondValue);It will return the number of leading zeros in secondValue.
  • Print the values.
package com.technicalkeeda;

public class App {

    public static void main(String[] args) {

        Integer firstValue = 125;
        Integer secondValue = 42;

        int result1 = Integer.numberOfLeadingZeros(firstValue);
        int result2 = Integer.numberOfLeadingZeros(secondValue);

        System.out.println("Number of leading zeros in firstValue :- " + result1);
        System.out.println("Number of leading zeros in secondValue :- " + result2);
    }
}

output

Number of leading zeros in firstValue :- 25
Number of leading zeros in secondValue :- 26
Yashwant

Yashwant

Hi there! I am founder of technicalkeeda.com and programming enthusiast. My skills includes Java,J2EE, Spring Framework, Nodejs, PHP and lot more. If you have any idea that you would want me to develop? Lets connect: yashwantchavan[at][gmail.com]