當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Golang bits.OnesCount64()用法及代碼示例

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



相關用法


注:本文由純淨天空篩選整理自Kirti_Mangal大神的英文原創作品 bits.OnesCount64() Function in Golang with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。