Go语言提供了对位的内置支持,以借助bits包对预声明的无符号整数类型实现位计数和操作函数。该程序包提供OnesCount64()函数,用于查找a中的一位数。要访问OnesCount64()函数,您需要借助import关键字在程序中添加一个math /bits包。
用法:
func OnesCount64(a uint64) int
参数:此函数采用uint64类型的一个参数,即a。
返回值:此函数返回用于表示a的一位的总数。
范例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)
}
输出:
Total number of one bits that are used to represent 5:2
范例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)
}
输出:
OnesCount64(0000000000000000000000000000000000000000000000000000000000010111):= 4 OnesCount64(0000000000000000000000000000000000000000000000000000000000001101):= 3
相关用法
- Golang math.Lgamma()用法及代码示例
- Golang math.Float64bits()用法及代码示例
- Golang atomic.AddInt64()用法及代码示例
- Golang atomic.StoreInt64()用法及代码示例
- Golang reflect.FieldByIndex()用法及代码示例
- Golang string.Contains用法及代码示例
- Golang bits.Sub()用法及代码示例
- Golang io.PipeWriter.CloseWithError()用法及代码示例
- Golang time.Round()用法及代码示例
- Golang reflect.AppendSlice()用法及代码示例
- Golang reflect.ChanOf()用法及代码示例
- Golang flag.Bool()用法及代码示例
- Golang time.Sleep()用法及代码示例
- Golang time.Time.Year()用法及代码示例
- Golang reflect.DeepEqual()用法及代码示例
- Golang reflect.Indirect()用法及代码示例
- Golang reflect.CanAddr()用法及代码示例
- Golang reflect.CanInterface()用法及代码示例
- Golang reflect.CanSet()用法及代码示例
- Golang reflect.Cap()用法及代码示例
注:本文由纯净天空筛选整理自Kirti_Mangal大神的英文原创作品 bits.OnesCount64() Function in Golang with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。