当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Java lang.Long.numberOfTrailingZeros()用法及代码示例



java.lang.Long.numberOfTrailingZeros()是Java中的内置函数,该函数返回最低顺序设置位右侧的尾随零位数量。简单来说,它返回(position-1),其中position指的是从右边开始的第一个置位位。如果该数字不包含任何设置位(换句话说,如果该数字为零),则返回64。

用法:

public static long numberOfTrailingZeros(long num)
Parameters:
num - the number passed 
Returns:
the number of trailing zeros after the lowest-order set bit

例子:


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 

以下示例程序旨在说明java.lang.Long.numberOfTrailingZeros()函数:

程序1:

// 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)); 
    } 
}

输出:

Number of trailing zeros = 3
Number of trailing zeros = 0

程序2:下面的程序演示了传递负数时函数的使用。

// 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)); 
    } 
}

输出:

Number of trailing zeros = 2

当将十进制字符串值作为参数传递时,它将返回错误消息。
程序3:在参数中传递十进制值时。

// 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)); 
    } 
}

输出:

prog.java:16:error:incompatible types:possible lossy conversion from double to long
      System.out.println("Number of trailing zeros = "+Long.numberOfTrailingZeros(12.34));

程序4:在参数中传递字符串值时。

// 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")); 
    } 
}

输出:

prog.java:15:error:incompatible types:String cannot be converted to long
      System.out.println("Number of trailing zeros = "+Long.numberOfTrailingZeros("100"));


相关用法


注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 Java lang.Long.numberOfTrailingZeros() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。