Java lang.Long.numberOfTrailingZeros() method in Java with Examples Last Updated : 23 May, 2019 Comments Improve Suggest changes Like Article Like Report java.lang.Long.numberOfTrailingZeros() is a built-in function in Java which returns the number of trailing zero bits to the right of the lowest order set bit. In simple terms, it returns the (position-1) where position refers to the first set bit from the right. If the number does not contain any set bit(in other words, if the number is zero), it returns 64. Syntax: public static long numberOfTrailingZeros(long num) Parameters: num - the number passed Returns: the number of trailing zeros after the lowest-order set bit Examples: Input : 8 Output : 3 Explanation: Binary representation of 8 is 1000 No of trailing zeros to the right of the lowest-order set bit is 3. Input : 191 Output : 0 The program below illustrates the java.lang.Long.numberOfTrailingZeros() function: Program 1: java // Java program that demonstrates the // Long.numberOfTrailingZeros() function // include lang package import java.lang.*; public class GFG { public static void main(String[] args) { long l = 8; // returns the number of zero bits following the lowest-order // set bit System.out.println("Number of trailing zeros = " + Long.numberOfTrailingZeros(l)); // second example l = 25; System.out.println("Number of trailing zeros = " + Long.numberOfTrailingZeros(l)); } } Output: Number of trailing zeros = 3 Number of trailing zeros = 0 Program 2: The program below demonstrates the use of function when a negative number is passed. java // Java program that demonstrates the // Long.numberOfTrailingZeros() function // negative number // include lang package import java.lang.*; public class GFG { public static void main(String[] args) { long l = -12; // returns the number of zero bits following the lowest-order // set bit System.out.println("Number of trailing zeros = " + Long.numberOfTrailingZeros(l)); } } Output: Number of trailing zeros = 2 It returns an error message when a decimal a string value is passed as an argument. Program 3: When a decimal value is passed in argument. java // Java program that demonstrates the // Long.numberOfTrailingZeros() function // decimal value as an argument // include lang package import java.lang.*; public class GFG { public static void main(String[] args) { // returns the number of zero bits following the lowest-order // set bit System.out.println("Number of trailing zeros = " + Long.numberOfTrailingZeros(12.34)); } } Output: prog.java:16: error: incompatible types: possible lossy conversion from double to long System.out.println("Number of trailing zeros = "+Long.numberOfTrailingZeros(12.34)); Program 4: When a string value is passed in argument. java // java program that demonstrates the // Long.numberOfTrailingZeros() function // string value as an argument // include lang package import java.lang.*; public class GFG { public static void main(String[] args) { // returns the number of zero bits following the lowest-order // set bit System.out.println("Number of trailing zeros = " + Long.numberOfTrailingZeros("100")); } } Output: prog.java:15: error: incompatible types: String cannot be converted to long System.out.println("Number of trailing zeros = "+Long.numberOfTrailingZeros("100")); Comment More infoAdvertise with us Next Article Java lang.Long.numberOfTrailingZeros() method in Java with Examples gopaldave Follow Improve Article Tags : Misc Java Java-lang package Java-Functions java-math java-Long +2 More Practice Tags : JavaMisc Similar Reads Java lang.Long.numberOfLeadingZeros() method in Java with Examples java.lang.Long.numberOfLeadingZeros() is a built-in function in Java which returns the number of leading zero bits to the left of the highest order set bit. In simple terms, it returns the (64-position) where position refers to the highest order set bit from the right. If the number does not contain 3 min read Integer.numberOfTrailingZeros() Method in Java with Example The Java.lang.Integer.numberOfTrailingZeros() is the method which returns the total number of zero(0) bits following the lowest-order (ie.Rightmost or least significant "1" bit)one-bit in the two's complement binary representation of the specified integer value or we can say that it is the function 3 min read Java lang Long.toOctalString() Method with Examples The Java.lang.Long.toOctalString() function is a built-in function in Java that returns a string representation of the long argument as an unsigned integer in base 8. Syntax: public static int toOctalString(long num) Parameters: The function accepts a single mandatory parameter num, which is to be c 2 min read Java.lang.Long.valueOf() method with examples Java.lang.Long.valueOf() is a built-in method in Java of lang class that returns a Long object holding the value extracted from a specified String S when parsed with the radix that is given in the second argument. The first argument is interpreted as representing a signed long in the radix specified 2 min read Number.longValue() method in java with examples The Number.longValue() is an inbuilt method in Java of java.lang package. This method is used to convert any numeric value into a long type. This may involve rounding or truncation.What is longValue()?The longValue() method returns the value of a number after converting it to the long data type. Som 2 min read OptionalLong of(long) method in Java with examples The of(long) method help us to get an OptionalLong object which contains a long value which is passed as a parameter to this method. Syntax: public static OptionalLong of(long value) Parameters: This method accepts a long value as parameter that will be set to the returned OptionalLong object. Retur 1 min read Number.shortValue() method in java with examples The shortValue() is an inbuilt method in Java from the java.lang.Number class. This method is used to convert the current number object into a short primitive type. This may involve rounding or truncation because the short data type in Java is a 16-bit signed integer with a value range from -32,768 2 min read Java lang.Long.builtcount() method in Java with Examples java.lang.Long.bitCount() is a built in function in Java that returns the number of set bits in a binary representation of a number. It accepts a single mandatory parameter number whose number of set bits is returned. Syntax: public static long bitCount(long num) Parameters: num - the number passed 3 min read OptionalLong orElse(long) method in Java with examples The orElse(long) method helps us to get the value in this OptionalLong object. If a value is not present in this OptionalLong, then this method returns the value passed as the parameter. Syntax: public long orElse(long other) Parameters: This method accepts long the value to be returned, if no value 1 min read Duration ofNanos(long) method in Java with Examples The ofNanos(long) method of Duration Class in java.time package is used to get a duration in a 1 nanoSecond format. Syntax: public static Duration ofNanos(long nanoSeconds) Parameters: This method accepts a parameter nanoSeconds which is the number of nanoSeconds. It can be positive or negative. Ret 1 min read Like