Created
March 4, 2021 17:26
-
-
Save scorredoira/cc4ab70d58428d7dce32f291f4090485 to your computer and use it in GitHub Desktop.
set_bits.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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