使用 Go 对 CPU 个 ID 进行位掩码转换

Bitmasking conversion of CPU ids with Go

我有一个掩码,其中包含 cpu_ids 的二进制计数(0xA00000800000 用于 3 个 CPU),我想将其转换为逗号分隔的 string cpu_ids : "0,2,24".

我做了以下 Go 实现(我是 Go 初学者)。这是最好的方法吗?特别是字节缓冲区的处理似乎效率低下!

package main

import (
    "fmt"
    "os"
    "os/exec"
)

func main(){
    cpuMap     := "0xA00000800000"
    cpuIds     = getCpuIds(cpuMap)
    fmt.Println(cpuIds)
}

func getCpuIds(cpuMap string) string {
    // getting the cpu ids
    cpu_ids_i, _ := strconv.ParseInt(cpuMap, 0, 64) // int from string
    cpu_ids_b := strconv.FormatInt(cpu_ids_i, 2)    // binary as string

    var buff bytes.Buffer
    for i, runeValue := range cpu_ids_b {
        // take care! go returns code points and not the string    
        if runeValue == '1' {
            //fmt.Println(bitString, i)
            buff.WriteString(fmt.Sprintf("%d", i))
        }
        if (i+1 < len(cpu_ids_b)) && (runeValue == '1') {
            //fmt.Println(bitString)
            buff.WriteString(string(","))
        }

    }
    cpuIds := buff.String()
    // remove last comma
    cpuIds = cpuIds[:len(cpuIds)-1]
    //fmt.Println(cpuIds)
    return cpuIds
}

Returns:

"0,2,24"

你所做的实际上是从左到右输出二进制表示中 "1" 的索引,并从左开始索引计数(不常用)。

您可以使用位掩码和按位运算符实现相同的效果,而无需将其转换为二进制字符串。我会 return 一片索引而不是它的格式化字符串,更容易使用。

要测试最低(最右边)位是否为 1,您可以像 x&0x01 == 1 那样做,并且将整数按位向右移动:x >>= 1。移位后,最右边的位"disappears",之前的第2位变成第1位,所以可以用同样的逻辑再次测试。您可以循环直到数字大于 0(这意味着它仍然有 1 位)。

有关按位运算的更多示例,请参阅此问题:

当然如果我们测试最右边的位并右移,我们得到的位(索引)是反向顺序(与您想要的相比),并且索引被计算在内从右边开始,所以我们必须在 return 计算结果之前更正它。

所以解决方案如下所示:

func getCpuIds(cpuMap string) (r []int) {
    ci, err := strconv.ParseInt(cpuMap, 0, 64)
    if err != nil {
        panic(err)
    }

    count := 0
    for ; ci > 0; count, ci = count+1, ci>>1 {
        if ci&0x01 == 1 {
            r = append(r, count)
        }
    }

    // Indices are from the right, correct it:
    for i, v := range r {
        r[i] = count - v - 1
    }
    // Result is in reverse order:
    for i, j := 0, len(r)-1; i < j; i, j = i+1, j-1 {
        r[i], r[j] = r[j], r[i]
    }

    return
}

输出(在 Go Playground 上尝试):

[0 2 24]

如果出于某种原因您需要以逗号分隔的结果 string,您可以通过以下方式获得该结果:

buf := &bytes.Buffer{}
for i, v := range cpuIds {
    if i > 0 {
        buf.WriteString(",")
    }
    buf.WriteString(strconv.Itoa(v))
}
cpuIdsStr := buf.String()
fmt.Println(cpuIdsStr)

输出(在 Go Playground 上尝试):

0,2,24