bits.LeadingZeros() Golang 中的函数用于查找给定数字中前导零位的数量。如果给定的数字等于 0,则此函数将返回 UintSize。要访问此函数,需要在程序中导入 math/bits 包。
Syntax:
Parameters: This function takes one parameter of uint type, i.e., x.
Return Value: This function returns a total number of leading zero bits in x.
示例 1:
func LeadingZeros64(x uint) int
输出:
// 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)
}
示例 2:
Total number of leading zero bits: 61
输出:
// 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)
}