package main
import "fmt"
func hitungNomorBit(angka int, nomorBit int) interface{} {
if nomorBit != 0 && nomorBit != 1 {
return nil
}
if angka == 0 {
if nomorBit == 0 {
return 1
}
return 0
}
count := 0
temp := angka
for temp > 0 {
bit := temp % 2
if bit == nomorBit {
count++
}
temp = temp / 2
}
return count
}
func main() {
fmt.Println("hitungNomorBit(13, 0):", hitungNomorBit(13, 0))
fmt.Println("hitungNomorBit(13, 1):", hitungNomorBit(13, 1))
fmt.Println("hitungNomorBit(13, 2):", hitungNomorBit(13, 2))
fmt.Println("\nTesting dengan angka lain:")
fmt.Println("hitungNomorBit(7, 0):", hitungNomorBit(7, 0))
fmt.Println("hitungNomorBit(7, 1):", hitungNomorBit(7, 1))
fmt.Println("hitungNomorBit(8, 0):", hitungNomorBit(8, 0))
fmt.Println("hitungNomorBit(8, 1):", hitungNomorBit(8, 1))
fmt.Println("hitungNomorBit(0, 0):", hitungNomorBit(0, 0))
fmt.Println("hitungNomorBit(0, 1):", hitungNomorBit(0, 1))
}
cGFja2FnZSBtYWluCgppbXBvcnQgImZtdCIKCmZ1bmMgaGl0dW5nTm9tb3JCaXQoYW5na2EgaW50LCBub21vckJpdCBpbnQpIGludGVyZmFjZXt9IHsKICAgIGlmIG5vbW9yQml0ICE9IDAgJiYgbm9tb3JCaXQgIT0gMSB7CiAgICAgICAgcmV0dXJuIG5pbAogICAgfQogICAgCiAgICBpZiBhbmdrYSA9PSAwIHsKICAgICAgICBpZiBub21vckJpdCA9PSAwIHsKICAgICAgICAgICAgcmV0dXJuIDEKICAgICAgICB9CiAgICAgICAgcmV0dXJuIDAKICAgIH0KICAgIAogICAgY291bnQgOj0gMAogICAgdGVtcCA6PSBhbmdrYQogICAgCiAgICBmb3IgdGVtcCA+IDAgewogICAgICAgIGJpdCA6PSB0ZW1wICUgMgogICAgICAgIGlmIGJpdCA9PSBub21vckJpdCB7CiAgICAgICAgICAgIGNvdW50KysKICAgICAgICB9CiAgICAgICAgdGVtcCA9IHRlbXAgLyAyCiAgICB9CiAgICAKICAgIHJldHVybiBjb3VudAp9CgpmdW5jIG1haW4oKSB7CiAgICBmbXQuUHJpbnRsbigiaGl0dW5nTm9tb3JCaXQoMTMsIDApOiIsIGhpdHVuZ05vbW9yQml0KDEzLCAwKSkKICAgIGZtdC5QcmludGxuKCJoaXR1bmdOb21vckJpdCgxMywgMSk6IiwgaGl0dW5nTm9tb3JCaXQoMTMsIDEpKQogICAgZm10LlByaW50bG4oImhpdHVuZ05vbW9yQml0KDEzLCAyKToiLCBoaXR1bmdOb21vckJpdCgxMywgMikpCiAgICAKICAgIGZtdC5QcmludGxuKCJcblRlc3RpbmcgZGVuZ2FuIGFuZ2thIGxhaW46IikKICAgIGZtdC5QcmludGxuKCJoaXR1bmdOb21vckJpdCg3LCAwKToiLCBoaXR1bmdOb21vckJpdCg3LCAwKSkKICAgIGZtdC5QcmludGxuKCJoaXR1bmdOb21vckJpdCg3LCAxKToiLCBoaXR1bmdOb21vckJpdCg3LCAxKSkKICAgIGZtdC5QcmludGxuKCJoaXR1bmdOb21vckJpdCg4LCAwKToiLCBoaXR1bmdOb21vckJpdCg4LCAwKSkKICAgIGZtdC5QcmludGxuKCJoaXR1bmdOb21vckJpdCg4LCAxKToiLCBoaXR1bmdOb21vckJpdCg4LCAxKSkKICAgIGZtdC5QcmludGxuKCJoaXR1bmdOb21vckJpdCgwLCAwKToiLCBoaXR1bmdOb21vckJpdCgwLCAwKSkKICAgIGZtdC5QcmludGxuKCJoaXR1bmdOb21vckJpdCgwLCAxKToiLCBoaXR1bmdOb21vckJpdCgwLCAxKSkKfQ==