fork download
  1. def hitungNomorBit(angka, nomorBit):
  2. if nomorBit != 0 and nomorBit != 1:
  3. return None
  4.  
  5. biner = []
  6.  
  7. if angka == 0:
  8. biner = [0]
  9.  
  10. while angka > 0:
  11. sisa = angka % 2
  12. biner.insert(0, sisa)
  13. angka = angka // 2
  14.  
  15. return biner.count(nomorBit)
  16.  
  17. # pengujian contoh
  18. print(hitungNomorBit(13, 0))
  19. print(hitungNomorBit(13, 1))
  20. print(hitungNomorBit(13, 2))
Success #stdin #stdout 0.08s 14084KB
stdin
Standard input is empty
stdout
1
3
None