Skip to content

Instantly share code, notes, and snippets.

@mohclips
Last active January 13, 2021 22:59
Show Gist options
  • Save mohclips/3dd83a98eacc2deae94d572a4cd14d34 to your computer and use it in GitHub Desktop.
Save mohclips/3dd83a98eacc2deae94d572a4cd14d34 to your computer and use it in GitHub Desktop.
split bitmask into names
package main
import (
"fmt"
)
var FlagsToNames = map[int]string{
0x0: "read",
0x1: "write",
0x2: "deny",
0x4: "list",
0x8: "delete",
0x10: "policy",
0x20: "quota",
0x40: "lock",
0x80: "bit 80",
}
func bitmaskToNames(k int, m map[int]string) []string {
var result []string
for i := 0; i < len(m); i++ {
pos := k&(1<<i)
if pos != 0 {
result = append(result, m[pos])
}
}
return result
}
func main() {
b := 0b11100011
fmt.Printf("%b\n",b)
fmt.Printf("%v\n",bitmaskToNames(b,FlagsToNames))
}
@mohclips
Copy link
Author

Output:

11100011
[write deny quota lock bit 80]

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