Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save scorredoira/cc4ab70d58428d7dce32f291f4090485 to your computer and use it in GitHub Desktop.
Save scorredoira/cc4ab70d58428d7dce32f291f4090485 to your computer and use it in GitHub Desktop.
set_bits.go
package main
import (
"fmt"
)
func main() {
var i uint32
// set the first 5 bits with the num 12
i = 12
fmt.Printf("%016b\n", i)
// 0000000000010110
// also set from bit 5 the num 13
i = 12 | 13<<5
fmt.Printf("%016b\n", i)
// also set from bit 10 the num 14
i = 12 | 13<<5 | 14<<10
fmt.Printf("%016b\n", i)
x := (i >> 0) & ((1 << 5) - 1)
fmt.Println("-------------")
fmt.Printf("%016b\n", 1<<5)
fmt.Printf("%016b\n", ((1 << 5) - 1))
fmt.Println(x)
x = (i >> 5) & ((1 << 5) - 1)
fmt.Println(x)
x = (i >> 10) & ((1 << 5) - 1)
fmt.Println(x)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment