#include <iostream>
using namespace std;

int hitungNomorBit(int angka, int nomorBit);
string intToBin(int angka);


int main() {
	cout << "hitungNomorBit(13, 0): " << hitungNomorBit(13, 0) << endl;
	cout << "hitungNomorBit(13, 1): " << hitungNomorBit(13, 1) << endl;
	cout << "hitungNomorBit(13, 2): " << hitungNomorBit(13, 2) << endl;
	cout << "hitungNomorBit(128, 0): " << hitungNomorBit(128, 0) << endl;
	cout << "hitungNomorBit(128, 1): " << hitungNomorBit(128, 1) << endl;
	return 0;
}

int hitungNomorBit(int angka, int nomorBit)
{
	string bin {intToBin(angka)};
	int count {0};
	
	if (nomorBit == 0 || nomorBit == 1)
	{
		for (char c: bin)
		{
			if (c - '0' == nomorBit)
			{
				count++;
			}
		}
		return count;
	}
	else
	{
		return -1; // -1 dipakai sebagai substitut dari null
	}
}

string intToBin(int angka)
{
	string out {};
	int sisa {0};
	
	while (angka != 0)
	{
		sisa = angka % 2;
		angka /= 2;
		out.append(1, sisa + '0');
	}
	
	return out;
}