golang simple bit operation example

  • 2020-06-01 09:59:20
  • OfStack

This example illustrates the simple bit operation of golang. I will share it with you for your reference as follows:

// http://play.golang.org/p/idG7Ri_krU
package main
import "fmt"
func main() {
    var n uint8 = 6
    fmt.Printf("%s\n%08b\n%08b\n\n", "6<<1 Shift to the left 1 position ", n, n<<1)
    fmt.Printf("%s\n%08b\n%08b\n\n", "6>>1 Moves to the right 1 position ", n, n>>1)
    fmt.Printf("%s\n%08b\n%08b\n\n", "^6 Who take the ", n, ^n)
    fmt.Printf("%s\n%08b\n%08b\n%08b\n\n", "6&5 with ", n, 5, n&5)
    fmt.Printf("%s\n%08b\n%08b\n%08b\n\n", "6|5 or ", n, 5, n|5)
    fmt.Printf("%s\n%08b\n%08b\n%08b\n\n", "6^5 A vision ", n, 5, n^5)
}

I hope this article has been helpful to you in programming Go.


Related articles: