Skip to content

Instantly share code, notes, and snippets.

@jmvrbanac
Created February 24, 2015 00:50
Show Gist options
  • Save jmvrbanac/905c84ac2033d741372d to your computer and use it in GitHub Desktop.
Save jmvrbanac/905c84ac2033d741372d to your computer and use it in GitHub Desktop.
Playing with bitflags in rust
#[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
}
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