def konversiDesimalBiner(angka: int):
if angka == 0:
return "0"
biner = ""
while(angka > 0):
biner = str(angka%2) + biner
angka //= 2
return biner
def hitungNomorBit(angka: int, nomorBit: int):
# cek
if(nomorBit != 0 and nomorBit != 1):
return None
biner = konversiDesimalBiner(angka)
count = 0
for bit in biner:
if(bit == str(nomorBit)):
count += 1
return count
print(hitungNomorBit(13, 0))
print(hitungNomorBit(13, 1))
print(hitungNomorBit(13, 2))
CmRlZiBrb252ZXJzaURlc2ltYWxCaW5lcihhbmdrYTogaW50KToKICAgIGlmIGFuZ2thID09IDA6CiAgICAgICAgcmV0dXJuICIwIgogICAgCiAgICBiaW5lciA9ICIiCiAgICB3aGlsZShhbmdrYSA+IDApOgogICAgICAgIGJpbmVyID0gc3RyKGFuZ2thJTIpICsgYmluZXIKICAgICAgICBhbmdrYSAvLz0gMgoKICAgIHJldHVybiBiaW5lcgoKZGVmIGhpdHVuZ05vbW9yQml0KGFuZ2thOiBpbnQsIG5vbW9yQml0OiBpbnQpOgogICAgIyBjZWsgCiAgICBpZihub21vckJpdCAhPSAwIGFuZCBub21vckJpdCAhPSAxKToKICAgICAgICByZXR1cm4gTm9uZQogICAgCiAgICBiaW5lciA9IGtvbnZlcnNpRGVzaW1hbEJpbmVyKGFuZ2thKQogICAgY291bnQgPSAwCiAgICBmb3IgYml0IGluIGJpbmVyOgogICAgICAgIGlmKGJpdCA9PSBzdHIobm9tb3JCaXQpKToKICAgICAgICAgICAgY291bnQgKz0gMQoKICAgIHJldHVybiBjb3VudAoKcHJpbnQoaGl0dW5nTm9tb3JCaXQoMTMsIDApKQpwcmludChoaXR1bmdOb21vckJpdCgxMywgMSkpCnByaW50KGhpdHVuZ05vbW9yQml0KDEzLCAyKSk=