fork download
  1. import math
  2.  
  3. # unsigned int jika positif
  4. # sign int jika negatif
  5. def hitungNomorBit(angka: int, nomorBit: int):
  6. bit = ""
  7. result = 0
  8. power = 64
  9. if (angka != 0):
  10. if (angka > 0):
  11. while angka < int(math.pow(2, power)):
  12. power -= 1
  13. else:
  14. while angka > -int(math.pow(2, power)):
  15. power -= 1
  16. angka += int(math.pow(2, power+1))
  17. bit += "1"
  18. while power >= 0:
  19. if (angka - int(math.pow(2, power)) >= 0):
  20. bit += "1"
  21. angka -= int(math.pow(2, power))
  22. else:
  23. bit += "0"
  24. power -= 1
  25.  
  26. for dec in bit:
  27. if (dec == str(nomorBit)):
  28. result += 1
  29.  
  30. elif (angka == 0 and nomorBit == 0):
  31. bit += "0"
  32. result = 1
  33.  
  34. if (result > 0):
  35. return result
  36. else:
  37. return "null"
  38.  
  39.  
  40. if __name__ == "__main__":
  41. print(hitungNomorBit(13, 0))
  42. print(hitungNomorBit(13, 1))
  43. print(hitungNomorBit(13, 2))
  44.  
Success #stdin #stdout 0.03s 9984KB
stdin
Standard input is empty
stdout
1
3
null