Created
February 24, 2015 00:50
-
-
Save jmvrbanac/905c84ac2033d741372d to your computer and use it in GitHub Desktop.
Playing with bitflags in rust
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
| #[macro_use] | |
| extern crate bitflags; | |
| mod something; | |
| fn main() { | |
| let e1 = something::FLAG_A | something::FLAG_C; | |
| let e2 = something::FLAG_B | something::FLAG_C; | |
| assert!((e1 | e2) == something::FLAG_ABC); // union | |
| assert!((e1 & e2) == something::FLAG_C); // intersection | |
| assert!((e1 - e2) == something::FLAG_A); // set difference | |
| assert!(!e2 == something::FLAG_A); // set complement | |
| } |
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
| bitflags! { | |
| flags Flags: u32 { | |
| const FLAG_A = 0b00000001, | |
| const FLAG_B = 0b00000010, | |
| const FLAG_C = 0b00000100, | |
| const FLAG_ABC = FLAG_A.bits | |
| | FLAG_B.bits | |
| | FLAG_C.bits, | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment