fork download
  1. def hitungNomorBit(angka, nomorBit):
  2. if nomorBit not in (0, 1):
  3. return None
  4.  
  5. if angka == 0:
  6. if nomorBit == 0:
  7. return 1
  8. else:
  9. return 0
  10.  
  11. jumlah = 0
  12.  
  13. while angka > 0:
  14. bit = angka % 2
  15.  
  16. if bit == nomorBit:
  17. jumlah = jumlah + 1
  18.  
  19. angka = angka // 2
  20.  
  21. return jumlah
  22.  
  23.  
  24. # Input angka desimal
  25. angka = int(input())
  26.  
  27. # Input bit yang ingin dihitung (0 atau 1)
  28. nomorBit = int(input())
  29.  
  30. print(hitungNomorBit(angka, nomorBit))
  31.  
Success #stdin #stdout 0.08s 14160KB
stdin
13
1
stdout
3