Golang bits.Reverse8() 函数的应用及示例
Go语言提供了内置支持的 bits 包,可实现针对预声明的无符号整数类型的位计数和操作函数。该包提供了 Reverse8() 函数 ,用于查找 a 值的反转顺序。要访问 Reverse8() 函数,您需要使用 import 关键字在程序中添加 math/bits 包。
语法:
func Reverse8(a uint8) uint8
参数: 该函数接受一个 uint8 类型参数 a。
返回值: 该函数返回其位反转的 a 值。
示例 1:
// Golang program to illustrate bits.Reverse8() Function
package main
import (
"fmt"
"math/bits"
)
// Main function
func main() {
// Using Reverse8() function
a := bits.Reverse8(5)
fmt.Printf("Reverse order of %d: %b", 5, a)
}
输出:
Reverse order of 5: 10100000
示例 2:
// Golang program to illustrate bits.Reverse8() Function
package main
import (
"fmt"
"math/bits"
)
// Main function
func main() {
// Using Reverse8() function
a1 := bits.Reverse8(9)
fmt.Printf("Reverse8(%08b) := %b\n", 9, a1)
a2 := bits.Reverse8(13)
fmt.Printf("Reverse8(%08b) := %b\n", 13, a2)
}
输出:
Reverse8(00001001) := 10010000
Reverse8(00001101) := 10110000
极客教程