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


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

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

用法:
func LeadingZeros8(x uint8) int

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

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

範例1:



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

輸出:

Total number of leading zero bits: 5

範例2:

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

輸出:

Total number of leading zero bits: 8

相關用法


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