Go 语言提供了对位的内置支持,以在位包的帮助下为预先声明的无符号整数类型实现位计数和操作功能。该包提供了TrailingZeros16()函数,用于查找 a 中尾随零位的数量,结果为 16 表示 a == 0。要访问 TrailingZeros16()函数,您需要在程序中添加一个 math/bits 包import 关键字的帮助。
句法:
func TrailingZeros16(a uint16) int
参数:该函数接受一个 uint16 类型的参数,即 a。
返回值:此函数返回 a 中尾随零位的总数。
示例 1:
// Golang program to illustrate
// bits.TrailingZeros16() Function
package main
import (
"fmt"
"math/bits"
)
// Main function
func main() {
// Using TrailingZeros16() function
a := bits.TrailingZeros16(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.TrailingZeros16() Function
package main
import (
"fmt"
"math/bits"
)
// Main function
func main() {
// Using TrailingZeros16() function
a1 := bits.TrailingZeros16(8)
fmt.Printf("TrailingZeros16(%016b)) := %d\n", 8, a1)
a2 := bits.TrailingZeros16(13)
fmt.Printf("TrailingZeros16(%016b) := %d\n", 13, a2)
}
输出:
TrailingZeros16(0000000000001000)) := 3
TrailingZeros16(0000000000001101) := 0