Skip to content

Instantly share code, notes, and snippets.

@xh3b4sd
Created October 25, 2022 13:00
Show Gist options
  • Save xh3b4sd/9bff3ce1b98287fd9521b724a86297c8 to your computer and use it in GitHub Desktop.
Save xh3b4sd/9bff3ce1b98287fd9521b724a86297c8 to your computer and use it in GitHub Desktop.
golang bit operators
package main
import (
"encoding/binary"
"fmt"
)
func main() {
var zer byte = 0b0
var one byte = 0b1
fmt.Printf("zer %b\n", zer)
fmt.Printf("one %b\n", one)
fmt.Printf("\n")
fmt.Printf("one & one %b\n", one&one)
fmt.Printf("one & zer %b\n", one&zer)
fmt.Printf("zer & one %b\n", zer&one)
fmt.Printf("zer & zer %b\n", zer&zer)
fmt.Printf("\n")
fmt.Printf("one | one %b\n", one|one)
fmt.Printf("one | zer %b\n", one|zer)
fmt.Printf("zer | one %b\n", zer|one)
fmt.Printf("zer | zer %b\n", zer|zer)
fmt.Printf("\n")
fmt.Printf("one ^ one %b\n", one^one)
fmt.Printf("one ^ zer %b\n", one^zer)
fmt.Printf("zer ^ one %b\n", zer^one)
fmt.Printf("zer ^ zer %b\n", zer^zer)
fmt.Printf("\n")
fmt.Printf("size zer %d\n", binary.Size(zer))
fmt.Printf("size one %d\n", binary.Size(one))
}
@xh3b4sd
Copy link
Author

xh3b4sd commented Oct 25, 2022

$ go run main.go
zer     0
one     1

one & one     1
one & zer     0
zer & one     0
zer & zer     0

one | one     1
one | zer     1
zer | one     1
zer | zer     0

one ^ one     0
one ^ zer     1
zer ^ one     1
zer ^ zer     0

size zer      1
size one      1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment