Last active
January 13, 2021 22:59
-
-
Save mohclips/3dd83a98eacc2deae94d572a4cd14d34 to your computer and use it in GitHub Desktop.
split bitmask into names
This file contains hidden or 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" | |
) | |
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)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: