BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Integer numberOfLeadingZeros() Method

By Chaitanya Singh | Filed Under: java

The numberOfLeadingZeros() method of Integer class, returns number of 0’s bits before the first one-bit in the binary representation of the given int number. If number is zero (there are no one-bit), then it returns 32.

Syntax of numberOfLeadingZeros() method

public static int numberOfLeadingZeros(int i)

numberOfLeadingZeros() Parameters

  • i – An int value whose number of leading zeroes needs to be determined.

numberOfLeadingZeros() Return Value

  • Returns number of zero bits preceding the highest-order (leftmost) one bit

Supported versions: Java 1.5 and onwards.

Example 1

Here, we are counting the number of zero bits before first one-bit in the given number. The point to remember is that each number is represented as 32 bits binary number. The binary representation of decimal number 20 is 10100, however when this is represented as a 32 bits binary number then it is proceeded with 27 zero bits.

This is why this method returns 32 for the integer value 0 as all 32 bits are zeroes.

class JavaExample {
  public static void main(String args[]) {
    //binary equivalent: 
    // 0000 0000 0000 0000 0000 0000 0001 0100
    int i = 20;
    System.out.print("Number of leading zero bits: ");
    System.out.print(Integer.numberOfLeadingZeros(i));
  }
}

Output:

Java Integer numberOfLeadingZeros Output1

Example 2

The number of leading zeroes are returned as zero for the negative numbers as the most significant bit is 1 for negative numbers.

class JavaExample {
  public static void main(String args[]) {
    //MSF (Most significant bit) is 1
    int i = -100;
    System.out.print("Number of leading zero bits: ");
    System.out.print(Integer.numberOfLeadingZeros(i));
  }
}

Output:

Java Integer numberOfLeadingZeros Output2

Recommended Posts

  • Java Integer longValue()
  • Java Integer intValue()
  • Java Integer min()
  • Java Integer max()
❮ Java Integer

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap