位。Len8()函数在 Golang 中的应用示例

原文:https://www . geesforgeks . org/bits-len 8-function-in-golang-with-examples/

Go 语言在 bits 包的帮助下,为 bits 提供内置支持,以实现预声明的无符号整数类型的位计数和操作功能。这个包提供了 Len8()函数,用来求表示 a 所需的最小位数,对于 a == 0,结果为 0。要访问 Len8()函数,您需要借助 import 关键字在程序中添加一个数学/位包。

语法:

func Len8(a uint8) (n int)

参数:该函数取 uint8 类型的一个参数,即 a。

返回值:该函数返回表示一个

例 1:

// Golang program to illustrate bits.Len8() Function
package main

import (
    "fmt"
    "math/bits"
)

// Main function
func main() {

    // Using Len8() function
    a := bits.Len8(3)
    fmt.Printf("The minimum number of bits "+
        "required to represent %d: %d", 3, a)

}

输出:

The minimum number of bits required to represent 3: 2

例 2:

// Golang program to illustrate bits.Len8() Function
package main

import (
    "fmt"
    "math/bits"
)

// Main function
func main() {

    // Using Len8() function
    a1 := bits.Len8(2)
    fmt.Printf("Len8(%08b) = %d\n", 2, a1)

    a2 := bits.Len8(12)
    fmt.Printf("Len8(%08b) = %d\n", 12, a2)

}

输出:

Len8(00000010) = 2
Len8(00001100) = 4