Last active
October 14, 2018 19:22
-
-
Save enthus1ast/0993a76ec8e6c22d09bc0030eac6a159 to your computer and use it in GitHub Desktop.
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
## Store multiple "bool" values in an integer | |
# http://blog.millermedeiros.com/using-integers-to-store-multiple-boolean-values/ | |
type foos = enum | |
foo = 1.shl 0, # 1 | |
baa = 1.shl 1, # 2 | |
baz = 1.shl 2 # 4 | |
echo foo.int | |
echo baa.int | |
echo baz.int | |
let fooAndBaa = foo.int or baa.int # compose an option | |
echo "fooAndBaa: ", fooAndBaa | |
assert (fooAndBaa.int and foo.int) > 0 == true | |
assert (fooAndBaa.int and baa.int) > 0 == true | |
assert (fooAndBaa.int and baz.int) > 0 == false | |
let fooAndBaaAndBaz = fooAndBaa.int or baz.int # add another option to the int | |
assert (fooAndBaaAndBaz.int and foo.int) > 0 == true | |
assert (fooAndBaaAndBaz.int and baa.int) > 0 == true | |
assert (fooAndBaaAndBaz.int and baz.int) > 0 == true | |
let fooAndBaz = fooAndBaaAndBaz.int xor baa.int # remove an option from the int | |
assert (fooAndBaz.int and foo.int) > 0 == true | |
assert (fooAndBaz.int and baa.int) > 0 == false | |
assert (fooAndBaz.int and baz.int) > 0 == true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment