def hitungNomorBit (angka, nomorBit):
# validasi input
if angka < 0 or nomorBit < 0:
return None
# konversi angka desimal ke biner
biner = []
while angka > 0:
biner.insert(0, angka % 2)
angka = angka // 2
# jika nomor bit melebihi panjang biner, kembalikan none
if nomorBit >= len(biner) or biner[nomorBit] == 0:
return None
# hitung jumlah keseluruhan bit di posisi nomorBit
jumlah = 0
for i in range(0, nomorBit + 1):
if biner[i] == 1:
jumlah += 2 ** (i)
return jumlah
print(hitungNomorBit(13, 3))
ZGVmIGhpdHVuZ05vbW9yQml0IChhbmdrYSwgbm9tb3JCaXQpOgoJIyB2YWxpZGFzaSBpbnB1dAoJaWYgYW5na2EgPCAwIG9yIG5vbW9yQml0IDwgMDoKCQlyZXR1cm4gTm9uZQoJCgkjIGtvbnZlcnNpIGFuZ2thIGRlc2ltYWwga2UgYmluZXIKCWJpbmVyID0gW10KCXdoaWxlIGFuZ2thID4gMDoKCQliaW5lci5pbnNlcnQoMCwgYW5na2EgJSAyKQoJCWFuZ2thID0gYW5na2EgLy8gMgoJCgkjIGppa2Egbm9tb3IgYml0IG1lbGViaWhpIHBhbmphbmcgYmluZXIsIGtlbWJhbGlrYW4gbm9uZQoJaWYgbm9tb3JCaXQgPj0gbGVuKGJpbmVyKSBvciBiaW5lcltub21vckJpdF0gPT0gMDoKCQlyZXR1cm4gTm9uZQoJCQoJIyBoaXR1bmcganVtbGFoIGtlc2VsdXJ1aGFuIGJpdCBkaSBwb3Npc2kgbm9tb3JCaXQKCWp1bWxhaCA9IDAKCWZvciBpIGluIHJhbmdlKDAsIG5vbW9yQml0ICsgMSk6CgkJaWYgYmluZXJbaV0gPT0gMToKCQkJanVtbGFoICs9IDIgKiogKGkpCgkJCQoJcmV0dXJuIGp1bWxhaAoJCnByaW50KGhpdHVuZ05vbW9yQml0KDEzLCAzKSk=