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
}