package main
import "fmt"
func hitungNomorBit(angka, nomorBit int) *int {
// Membuat array untuk menyimpan representasi biner
biner := make([]int, 32) // Asumsi maksimal 32 bit
index := 0
// Handle kasus khusus untuk angka 0
if angka == 0 {
if nomorBit == 0 {
result := 0
return &result
}
return nil
}
// Konversi ke biner
tempAngka := angka
for tempAngka > 0 {
biner[index] = tempAngka % 2
tempAngka = tempAngka / 2
index++
}
// Jika nomorBit lebih besar atau sama dengan panjang biner, return nil
if nomorBit >= index {
return nil
}
// Periksa nilai bit pada posisi yang diminta
result := 0
if biner[nomorBit] == 1 {
// Hitung jumlah bit 1 dari posisi 0 sampai nomorBit
for i := 0; i <= nomorBit; i++ {
if biner[i] == 1 {
result++
}
}
} else {
// Jika bit pada posisi yang diminta adalah 0
// Hanya hitung jumlah bit 1 sebelumnya
for i := 0; i < nomorBit; i++ {
if biner[i] == 1 {
result++
}
}
}
return &result
}
func main() {
fmt.Printf("hitungNomorBit(13, 0) = %v\n", *hitungNomorBit(13, 0)) // Seharusnya 1
fmt.Printf("hitungNomorBit(13, 1) = %v\n", *hitungNomorBit(13, 1)) // Seharusnya 1
fmt.Printf("hitungNomorBit(13, 2) = %v\n", *hitungNomorBit(13, 2)) // Seharusnya 2
}
cGFja2FnZSBtYWluCgppbXBvcnQgImZtdCIKCmZ1bmMgaGl0dW5nTm9tb3JCaXQoYW5na2EsIG5vbW9yQml0IGludCkgKmludCB7CiAgICAvLyBNZW1idWF0IGFycmF5IHVudHVrIG1lbnlpbXBhbiByZXByZXNlbnRhc2kgYmluZXIKICAgIGJpbmVyIDo9IG1ha2UoW11pbnQsIDMyKSAvLyBBc3Vtc2kgbWFrc2ltYWwgMzIgYml0CiAgICBpbmRleCA6PSAwCiAgICAKICAgIC8vIEhhbmRsZSBrYXN1cyBraHVzdXMgdW50dWsgYW5na2EgMAogICAgaWYgYW5na2EgPT0gMCB7CiAgICAgICAgaWYgbm9tb3JCaXQgPT0gMCB7CiAgICAgICAgICAgIHJlc3VsdCA6PSAwCiAgICAgICAgICAgIHJldHVybiAmcmVzdWx0CiAgICAgICAgfQogICAgICAgIHJldHVybiBuaWwKICAgIH0KICAgIAogICAgLy8gS29udmVyc2kga2UgYmluZXIKICAgIHRlbXBBbmdrYSA6PSBhbmdrYQogICAgZm9yIHRlbXBBbmdrYSA+IDAgewogICAgICAgIGJpbmVyW2luZGV4XSA9IHRlbXBBbmdrYSAlIDIKICAgICAgICB0ZW1wQW5na2EgPSB0ZW1wQW5na2EgLyAyCiAgICAgICAgaW5kZXgrKwogICAgfQogICAgCiAgICAvLyBKaWthIG5vbW9yQml0IGxlYmloIGJlc2FyIGF0YXUgc2FtYSBkZW5nYW4gcGFuamFuZyBiaW5lciwgcmV0dXJuIG5pbAogICAgaWYgbm9tb3JCaXQgPj0gaW5kZXggewogICAgICAgIHJldHVybiBuaWwKICAgIH0KICAgIAogICAgLy8gUGVyaWtzYSBuaWxhaSBiaXQgcGFkYSBwb3Npc2kgeWFuZyBkaW1pbnRhCiAgICByZXN1bHQgOj0gMAogICAgaWYgYmluZXJbbm9tb3JCaXRdID09IDEgewogICAgICAgIC8vIEhpdHVuZyBqdW1sYWggYml0IDEgZGFyaSBwb3Npc2kgMCBzYW1wYWkgbm9tb3JCaXQKICAgICAgICBmb3IgaSA6PSAwOyBpIDw9IG5vbW9yQml0OyBpKysgewogICAgICAgICAgICBpZiBiaW5lcltpXSA9PSAxIHsKICAgICAgICAgICAgICAgIHJlc3VsdCsrCiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9IGVsc2UgewogICAgICAgIC8vIEppa2EgYml0IHBhZGEgcG9zaXNpIHlhbmcgZGltaW50YSBhZGFsYWggMAogICAgICAgIC8vIEhhbnlhIGhpdHVuZyBqdW1sYWggYml0IDEgc2ViZWx1bW55YQogICAgICAgIGZvciBpIDo9IDA7IGkgPCBub21vckJpdDsgaSsrIHsKICAgICAgICAgICAgaWYgYmluZXJbaV0gPT0gMSB7CiAgICAgICAgICAgICAgICByZXN1bHQrKwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQogICAgCiAgICByZXR1cm4gJnJlc3VsdAp9CgpmdW5jIG1haW4oKSB7CiAgICBmbXQuUHJpbnRmKCJoaXR1bmdOb21vckJpdCgxMywgMCkgPSAldlxuIiwgKmhpdHVuZ05vbW9yQml0KDEzLCAwKSkgIC8vIFNlaGFydXNueWEgMQogICAgZm10LlByaW50ZigiaGl0dW5nTm9tb3JCaXQoMTMsIDEpID0gJXZcbiIsICpoaXR1bmdOb21vckJpdCgxMywgMSkpICAvLyBTZWhhcnVzbnlhIDEKICAgIGZtdC5QcmludGYoImhpdHVuZ05vbW9yQml0KDEzLCAyKSA9ICV2XG4iLCAqaGl0dW5nTm9tb3JCaXQoMTMsIDIpKSAgLy8gU2VoYXJ1c255YSAyCn0=