fork download
  1. def hitungNomorBit(angka, nomorBit):
  2. if nomorBit != 0 and nomorBit != 1:
  3. return None
  4.  
  5. if angka < 0:
  6. return None
  7.  
  8. if angka == 0:
  9. return 1 if nomorBit == 0 else 0
  10.  
  11. jumlah = 0
  12.  
  13. while angka > 0:
  14. bit = angka % 2
  15.  
  16. if bit == nomorBit:
  17. jumlah += 1
  18.  
  19. angka //= 2
  20.  
  21. return jumlah
  22.  
  23.  
  24. print(hitungNomorBit(13, 0))
  25. print(hitungNomorBit(13, 1))
  26. print(hitungNomorBit(13, 2))
  27.  
Success #stdin #stdout 0.08s 14000KB
stdin
Standard input is empty
stdout
1
3
None