Go 语言提供了对位的内置支持,以在位包的帮助下为预先声明的无符号整数类型实现位计数和操作功能。该包提供了TrailingZeros64()函数,用于查找 a 中尾随零位的数量,a == 0 的结果为 64。要访问 TrailingZeros64()函数,您需要在程序中添加一个 math/bits 包在 import 关键字的帮助下。
句法:
func TrailingZeros64(a uint64) int
参数:该函数接受一个 uint64 类型的参数,即 a。
返回值:此函数返回 a 中尾随零位的总数。
示例 1:
// Golang program to illustrate bits.TrailingZeros64() Function
package main
import (
"fmt"
"math/bits"
)
// Main function
func main() {
// Using TrailingZeros64() function
a := bits.TrailingZeros64(15)
fmt.Printf("Total number of trailing"+
" zero bits in %d: %d", 15, a)
}
输出:
Total number of trailing zero bits in 15: 0
示例 2:
// Golang program to illustrate bits.TrailingZeros64() Function
package main
import (
"fmt"
"math/bits"
)
// Main function
func main() {
// Using TrailingZeros64() function
a1 := bits.TrailingZeros64(8)
fmt.Printf("TrailingZeros64(%064b)) := %d\n", 8, a1)
a2 := bits.TrailingZeros64(24)
fmt.Printf("TrailingZeros64(%064b) := %d\n", 24, a2)
}
输出:
TrailingZeros64(0000000000000000000000000000000000000000000000000000000000001000)) := 3
TrailingZeros64(0000000000000000000000000000000000000000000000000000000000011000) := 3