Standard library / math/bits

bits.OnesCount()

Counts set bits (population count) in the unsigned word. Native uint width is architecture-dependent; local validation uses 64 bits. Expected stdout: '3\n'.

Formal Syntax & Structure

go syntaxgo
func OnesCount(x uint) int

Parameters & Attributes

NameTypeDescription
xuintFirst input operand (or sole input if unary).

Return Value

int

Counts set bits (population count) in the unsigned word. Native uint width is architecture-dependent; local validation uses 64 bits.

Working Example

Example Snippetgo
package main
import (  "fmt"  "math/bits")
func main() {  fmt.Println(bits.OnesCount(11))}
Go Reference