#include <iostream>
using namespace std;

int hitungNomorBit(int angka, int nomorBit) {
    // Validasi input nomorBit harus 0 atau 1
    if (nomorBit != 0 && nomorBit != 1) {
        return -1; // sebagai pengganti NULL
    }

    // Konversi angka desimal ke biner manual
    int biner[32]; // cukup untuk 32 bit
    int i = 0;
    while (angka > 0) {
        biner[i] = angka % 2;
        angka /= 2;
        i++;
    }

    // Hitung jumlah kemunculan nomorBit
    int jumlah = 0;
    for (int j = 0; j < i; j++) {
        if (biner[j] == nomorBit) {
            jumlah++;
        }
    }

    // Jika tidak ditemukan, anggap null -> return -1
    if (jumlah == 0) {
        return -1;
    }

    return jumlah;
}

int main() {
    cout << "hitungNomorBit(13, 0) = " << hitungNomorBit(13, 0) << endl; // 1
    cout << "hitungNomorBit(13, 1) = " << hitungNomorBit(13, 1) << endl; // 3
    cout << "hitungNomorBit(13, 2) = " << hitungNomorBit(13, 2) << endl; // -1 (NULL)
    return 0;
}
