Skip to content

Instantly share code, notes, and snippets.

@deeagle
Last active April 10, 2025 10:58
Show Gist options
  • Save deeagle/006b2d260e8931dd46cc84d734ecb04e to your computer and use it in GitHub Desktop.
Save deeagle/006b2d260e8931dd46cc84d734ecb04e to your computer and use it in GitHub Desktop.
Some bitoperations in go
package main
import (
"fmt"
"strconv"
)
func main() {
fmt.Println("hello")
fmt.Printf("^2 index = %3d %2d %2d %2d %1d %1d %1d %1d\n", 8, 7, 6, 5, 4, 3, 2, 1)
fmt.Printf("^2 result = %3d %2d %2d %2d %1d %1d %1d %1d\n\n", 128, 64, 32, 16, 8, 4, 2, 1)
var x, y int = 100, 90
fmt.Printf("x = %3d - %s\n", x, strconv.FormatInt(int64(x), 2))
fmt.Printf("y = %3d - %s\n\n", y, strconv.FormatInt(int64(y), 2))
xandy := x & y
xory := x | y
fmt.Printf("x & y = %3d - %s\n", xandy, strconv.FormatInt(int64(xandy), 2))
fmt.Printf("y | y = %3d - %s\n\n", xory, strconv.FormatInt(int64(xory), 2))
xy := x + y
xysl2 := xy >> 2
fmt.Printf("x + y = %3d - %9s\n", xy, strconv.FormatInt(int64(xy), 2))
fmt.Printf("x + y >> 2 = %3d - %9s\n", xysl2, strconv.FormatInt(int64(xysl2), 2))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment