fork download
  1. def hitung_nomor_bit(angka, nomor_bit) :
  2. if nomor_bit not in (0, 1):
  3. return None
  4. if angka == 0:
  5. return 1 if nomor_bit == 0 else 0
  6. hitung = 0
  7. while angka > 0:
  8. if angka %2 == nomor_bit :
  9. hitung += 1
  10. angka //= 2
  11. return hitung
  12.  
  13. print(hitung_nomor_bit(13, 0))
  14. print(hitung_nomor_bit(13, 1))
  15. print(hitung_nomor_bit(13, 2))
Success #stdin #stdout 0.07s 14216KB
stdin
Standard input is empty
stdout
1
3
None