Open In App

bits.OnesCount32() Function in Golang with Examples

Last Updated : 19 Apr, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report
Go language provides inbuilt support for bits to implement bit counting and manipulation functions for the predeclared unsigned integer types with the help of bits package. This package provides the OnesCount32() function which is used to find the number of one bits in a. To access the OnesCount32() function you need to add a math/bits package in your program with the help of the import keyword. Syntax:
func OnesCount32(a uint32) int
Parameters: This function takes one parameter of uint32 type, i.e., a. Return Value: This function returns the total number of one bits that are used to represent a. Example 1: C
// 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)

}
Output:
Total number of one bits that are used to represent 5: 2
Example 2: C
// 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)

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

Next Article
Article Tags :

Similar Reads