Skip to content
Related Articles

Related Articles

bits.OnesCount64() Function in Golang with Examples
  • Last Updated : 19 Apr, 2020
GeeksforGeeks - Summer Carnival Banner

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 OnesCount64() function which is used to find the number of one bits in a. To access OnesCount64() function you need to add a math/bits package in your program with the help of the import keyword.

Syntax: 

func OnesCount64(a uint64) int

Parameters: This function takes one parameter of uint64 type, i.e., a.

Return Value: This function returns the total number of one bits that are used to represent a.

Example 1 :






// Golang program to illustrate bits.OnesCount64() Function
package main
  
import (
    "fmt"
    "math/bits"
)
  
// Main function
func main() {
  
    // Using OnesCount64() function
    a := bits.OnesCount64(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 :




// Golang program to illustrate bits.OnesCount64() Function
package main
  
import (
    "fmt"
    "math/bits"
)
  
// Main function
func main() {
    // Using OnesCount64() function
    a1 := bits.OnesCount64(23)
    fmt.Printf("OnesCount64(%064b) := %d\n", 23, a1)
  
    a2 := bits.OnesCount64(13)
    fmt.Printf("OnesCount64(%064b) := %d\n", 13, a2)
  
}

Output:

OnesCount64(0000000000000000000000000000000000000000000000000000000000010111) := 4
OnesCount64(0000000000000000000000000000000000000000000000000000000000001101) := 3

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :