當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。