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


Golang bits.LeadingZeros()用法及代碼示例

bits.LeadingZeros() Golang中的函數用於查找給定數字中前導零位的數量。如果給定數字等於零,則此函數將返回UintSize。要訪問此函數,需要在程序中導入math /bits程序包。

用法:
func LeadingZeros64(x uint) int

參數:此函數采用uint類型的一個參數,即x。

返回值:此函數返回x中前導零位的總數。

範例1:



// Golang program to illustrate 
// bits.LeadingZeros() Function 
package main 
   
import ( 
    "fmt"
    "math/bits"
) 
   
// Main function 
func main() { 
   
    // Using LeadingZeros() function 
    x:= bits.LeadingZeros(5) 
    fmt.Println("Total number of leading zero bits:", x) 
   
}

輸出:

Total number of leading zero bits: 61

範例2:

// Golang program to illustrate 
// bits.LeadingZeros() Function 
package main 
   
import ( 
    "fmt"
    "math/bits"
) 
   
// Main function 
func main() { 
   
    // Using LeadingZeros() function 
    x:= bits.LeadingZeros(0) 
    fmt.Println("Total number of leading zero bits:", x) 
   
}

輸出:

Total number of leading zero bits: 64

相關用法


注:本文由純淨天空篩選整理自PranchalKatiyar大神的英文原創作品 bits.LeadingZeros() Function in Golang With Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。