bitwise and in go

To perform bitwise AND in Go, we can use the bitwise AND operator '&'. Here is an example:

main.go
package main

import "fmt"

func main() {
    a := 5    // binary 101
    b := 3    // binary 011
    c := a & b // binary 001 (result is 1)
    fmt.Printf("a & b = %d\n", c)
}
177 chars
11 lines

In the example above, we have two integer variables 'a' and 'b'. We use the bitwise AND operator '&' to perform bitwise AND on these values and store the result in a third variable 'c'. The binary representation of 'a' is 101 and the binary representation of 'b' is 011. The result of bitwise AND on these binary values is 001, which is equal to decimal 1. We print the value of 'c' using the 'fmt.Printf()' function.

Output:

main.go
a & b = 1
10 chars
2 lines

gistlibby LogSnag