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 numberOfTrailingZeros() Method

Last Updated: October 27, 2022 by Chaitanya Singh | Filed Under: java

The numberOfTrailingZeros() method of Integer class, returns number of zero-bits after the last one-bit in the binary representation of the given int number.

Returns 32 if there are no one-bit in the binary representation of the given int number (which means number is zero).

Syntax of numberOfTrailingZeros() method

public static int numberOfTrailingZeros(int i)

numberOfTrailingZeros() Parameters

  • i – An int value whose number of trailing zeroes to be computed.

numberOfTrailingZeros() Return Value

  • Returns number of zero bits following the lowest-order (rightmost) one bit.

Supported versions: Java 1.5 and onwards.

Example 1

Here, we have a number 20, whose number of trailing zeroes needs to be computed. There are only two zeroes after the last one-bit in the number so this method returns 2 as result.

class JavaExample {
  public static void main(String args[]) {
    //two's complement binary representation of 20:
    // 0000 0000 0000 0000 0000 0000 0001 0100
    int i = 20;
    System.out.print("Number of trailing zero bits: ");
    System.out.print(Integer.numberOfTrailingZeros(i));
  }
}

Output:

Java Integer numberOfTrailingZeros Output1

Example 2

Unlike numberOfLeadingZeros() method, where sign of the number changes the result. In this method, Number of trailing zeroes are same for positive and negative number, if the absolute values of the given numbers are same.

class JavaExample {
  public static void main(String args[]) {
    int i = 20;
    int i2 = -20;
    System.out.print("Trailing zero bits in +20: ");
    System.out.println(Integer.numberOfTrailingZeros(i));
    System.out.print("Trailing zero bits in -20: ");
    System.out.println(Integer.numberOfTrailingZeros(i2));
  }
}

Output:

Java Integer numberOfTrailingZeros Output2

Recommended Posts

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

Top Related Articles:

  1. Java Integer byteValue() Method
  2. Java Integer longValue() Method
  3. Java Integer highestOneBit() Method
  4. Java Integer hashCode() Method
  5. Ternary Operator in Java with Examples

Tags: Integer

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

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 – 2025 BeginnersBook . Privacy Policy . Sitemap