Skip to content

Instantly share code, notes, and snippets.

@mbrubeck
Created June 20, 2016 21:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbrubeck/22ec186759f36af65d324780248e5bd1 to your computer and use it in GitHub Desktop.
Save mbrubeck/22ec186759f36af65d324780248e5bd1 to your computer and use it in GitHub Desktop.
trait BitStorage {
fn set(&mut self, index: usize, value: bool);
}
impl BitStorage for u8 {
fn set(&mut self, index: usize, value: bool) {
*self |= (value as u8) << index
}
}
fn main() {
let mut x = 0u8;
x.set(4, true);
println!("{:08b}", x);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment