Number of 1 Bits
Solution in go

Convert the number to binary and while doing so keep counting the 1s

func hammingWeight(num uint32) int {
    total := 0

    for num > 0 {
        if num % 2 == 1 {
            total++
        }

        num = num / 2
    }

    return total
}
    TheDeveloperCafe © 2022-2024