fork download
  1. # your code goes here
  2. def hitungNomorBit(angka, nomorBit):
  3. if nomorBit != 0 and nomorBit != 1:
  4. return None
  5.  
  6. if angka == 0:
  7. return 1 if nomorBit == 0 else 0
  8.  
  9. jumlah_bit = 0
  10. temp_angka = angka
  11.  
  12. while temp_angka > 0:
  13. sisa = temp_angka % 2
  14. if sisa == nomorBit:
  15. jumlah_bit += 1
  16. temp_angka = temp_angka // 2
  17.  
  18. return jumlah_bit
  19.  
  20. # Representasi bilangan biner dari angka 13 adalah 1101
  21.  
  22. print(f"hitungNomorBit(13, 0) -> {hitungNomorBit(13, 0)}")
  23. print(f"hitungNomorBit(13, 1) -> {hitungNomorBit(13, 1)}")
  24. print(f"hitungNomorBit(13, 2) -> {hitungNomorBit(13, 2)}")
  25.  
  26. # Representasi biner dari 20 adalah 10100
  27. print("-" * 20)
  28. print(f"hitungNomorBit(20, 0) -> {hitungNomorBit(20, 0)}")
  29. print(f"hitungNomorBit(20, 1) -> {hitungNomorBit(20, 1)}")
Success #stdin #stdout 0.1s 14232KB
stdin
Standard input is empty
stdout
hitungNomorBit(13, 0) -> 1
hitungNomorBit(13, 1) -> 3
hitungNomorBit(13, 2) -> None
--------------------
hitungNomorBit(20, 0) -> 3
hitungNomorBit(20, 1) -> 2