2

カンマ区切りの cpu_ids:0xA00000800000に変換したいcpu_ids (3 つの CPU の場合) のバイナリ カウントを含むマスクがあります。string"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
}

戻り値:

"0,2,24"

4

1 に答える 1

3

あなたがしていることは、基本的"1"にバイナリ表現の 's のインデックスを左から右に出力し、左からインデックスカウントを開始することです (unusal)。

バイナリ文字列に変換せずに、ビットマスクとビットごとの演算子を使用して同じことを実現できます。そして、フォーマットされた文字列ではなく、操作しやすいインデックスのスライスを返します。

最下位 (右端) ビットが1であるかどうかをテストするには、 のようx&0x01 == 1にして、整数をビットごとに右にシフトしますx >>= 1。シフト後、右端のビットが「消え」、前の 2 番目のビットが 1 番目になるため、同じロジックで再度テストできます。数値が 0 より大きくなるまでループすることができます (つまり、ビットが 1 になることを意味します)。

ビット演算のその他の例については、この質問を参照してください:いくつかの演算子 "|"、"^"、"&"、"&^" の違い。ゴラン

もちろん、一番右のビットをテストして右にシフトすると、ビット (インデックス) はの順序で取得され (必要なものと比較して)、インデックスは右からカウントされるため、結果を返す前にこれを修正する必要があります。

したがって、ソリューションは次のようになります。

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
于 2016-10-25T09:12:01.683 に答える