How do I get the bits from a byte in golang?

go

Solution

You can see the visual representation of bits using `fmt.Sprintf("%08b", ...)` as others have already suggested.

However, if you want to use the bits in operations, such as for calculating the Hamming distance, you'll need to use bitwise operators.

To calculate the nth bit of a byte, you'll need to `bitwise AND` that byte with another byte whose nth bit is set to 1 and the rest to 0 (aka masking). In other words, that other byte (mask) is the number 2n-1.

For example, to find the 1st bit of the number 13 (00001101), we would have to mask it with 20 = 1 (00000001). We compare the output of performing bitwise AND on both numbers to the mask. If they are equal, it means that the nth bit is 1, otherwise it is 0. We continue like this and find all the bits. Illustrated in Go code:

fmt.Print(13 & 1) // Output: 1 -> 1
fmt.Print(13 & 2) // Output: 0 -> 0
fmt.Print(13 & 4) // Output: 4 -> 1
fmt.Print(13 & 8) // Output: 8 -> 1
// Not necessary to continue, but shown for the sake of the example
fmt.Print(13 & 16) // Output: 0 -> 0
fmt.Print(13 & 32) // Output: 0 -> 0
fmt.Print(13 & 64) // Output: 0 -> 0
fmt.Print(13 & 128) // Output: 0 -> 0

Therefore, 13 in binary is 00001101.

Here's a function I wrote recently for calculating the Hamming distance between two arrays of bytes. Just pass an array consisting of a single byte each in your case

func hamming(a, b []byte) (int, error) {
    if len(a) != len(b) {
        return 0, errors.New("a b are not the same length")
    }

    diff := 0
    for i := 0; i < len(a); i++ {
        b1 := a[i]
        b2 := b[i]
        for j := 0; j < 8; j++ {
            mask := byte(1 << uint(j))
            if (b1 & mask) != (b2 & mask) {
                diff++
            }
        }
    }
    return diff, nil
}

Go Playground: https://play.golang.org/p/O1EGdzDYAn

Problem

I'm trying to compute the Hamming distance between two byte's, such that `HammingDist(byte(255), byte(0)) == 8` I need the bits in each byte, but I can't find any function in any of the built-in packages for doing so. So, given `byte(1)` how do I get the bit representation 00000001?

Original source