fork download
  1. package main
  2. import "fmt"
  3.  
  4. func hitungNomorBit(angka int, nomorBit int) *int {
  5. if nomorBit != 0 && nomorBit != 1 {
  6. return nil }
  7.  
  8. if angka == 0 {
  9. count := 0
  10. if nomorBit == 0 {
  11. count = 1
  12. }
  13. return &count
  14. }
  15.  
  16. count := 0
  17. temp := angka
  18.  
  19. for temp > 0 {
  20. sisa := temp % 2
  21. if sisa == nomorBit {
  22. count++
  23. }
  24. temp = temp / 2
  25. }
  26. return &count
  27.  
  28. }
  29.  
  30. func main(){
  31. fmt.Println(*hitungNomorBit(13,0))
  32. fmt.Println(*hitungNomorBit(13,1))
  33. fmt.Println(hitungNomorBit(13,2))
  34. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
1
3
<nil>