位。Golang 中的 OnesCount32()函数及示例

原文:https://www . geesforgeks . org/bits-one count 32-function-in-golang-with-examples/

Go 语言在 bits 包的帮助下,为 bits 提供内置支持,以实现预声明的无符号整数类型的位计数和操作功能。这个包提供了onecount 32()函数,用来查找 a 中的一位数,要访问 onecount 32()函数,需要在 import 关键字的帮助下,在程序中添加一个数学/位包。

语法:

func OnesCount32(a uint32) int

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

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

例 1:

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

import (
    "fmt"
    "math/bits"
)

// Main function
func main() {

    a := bits.OnesCount32(5)
    fmt.Printf("Total number of one bits that"+
        " are used to represent %d: %d", 5, a)

}

输出:

Total number of one bits that are used to represent 5: 2

例 2:

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

import (
    "fmt"
    "math/bits"
)

// Main function
func main() {

    a1 := bits.OnesCount32(4)
    fmt.Printf("OnesCount32(%032b) := %d\n", 4, a1)

    a2 := bits.OnesCount32(13)
    fmt.Printf("OnesCount32(%032b) := %d\n", 13, a2)

}

输出:

OnesCount32(00000000000000000000000000000100) := 1
OnesCount32(00000000000000000000000000001101) := 3