bits.OnesCount16() Function in Golang with Examples
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 OnesCount16() function which is used to find the number of one bits in a. To access the OnesCount16() function you need to add a math/bits package in your program with the help of the import keyword.
Syntax:
func OnesCount16(a uint16) int
Parameters: This function takes one parameter of uint16 type, i.e., a.
Return Value: This function returns the total number of one bits that are used to represent a.
Example 1:
package main
import (
"fmt"
"math/bits"
)
func main() {
a := bits.OnesCount16(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:
package main
import (
"fmt"
"math/bits"
)
func main() {
a1 := bits.OnesCount16(4)
fmt.Printf( "OnesCount16(%016b) := %d\n" , 4, a1)
a2 := bits.OnesCount16(13)
fmt.Printf( "OnesCount16(%016b) := %d\n" , 13, a2)
}
|
Output:
OnesCount16(0000000000000100) := 1
OnesCount16(0000000000001101) := 3
Last Updated :
19 Apr, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...