Last active
August 12, 2023 14:28
-
-
Save zeddee/973ed4a3f58fed956b11d65e1b1aa1b3 to your computer and use it in GitHub Desktop.
Rust bitwise operations example
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
// Following: https://dev.to/somedood/bitmasks-a-very-esoteric-and-impractical-way-of-managing-booleans-1hlf | |
fn repr(label: &str, num: i8) { | |
/* Convenience function that prints `num`, which must be an i8, | |
as its 8-bit representation, and as an integer. | |
We use `{:#010b}` because when printing with `{:#}`, the number | |
of padded values on the left includes the initial `0b`. | |
E.g. `repr("Anything", 1)` prints this output: | |
```text | |
Any label: 0b00000001, Number: 1 | |
``` | |
*/ | |
println!("{label}: \t{:#010b}, Number: {}", num, num); | |
} | |
fn main() { | |
let num1: i8 = 0b1010; // 10 | |
let num2: i8 = 0b1111; // 15 | |
println!("num1: {:?}", num1); | |
println!("num2: {:?}", num2); | |
println!("====== std::ops::Not"); | |
repr("!num1", !num1); | |
repr("!num2", !num2); | |
println!("====== num1 & num2"); | |
{ | |
let res = num1 & num2; | |
repr("Result", res); | |
} | |
println!("====== num1 | num2"); | |
{ | |
let res = num1 | num2; | |
repr("Result", res); | |
} | |
println!("====== num1 ^ num2"); | |
{ | |
let res = num1 ^ num2; | |
repr("Result", res); | |
} | |
println!("====== num1 bitshift left by 1"); | |
{ | |
let res = num1 << 1; | |
repr("Original", num1); | |
repr("Result", res); | |
} | |
println!("====== num1 bitshift left by 2"); | |
{ | |
let res = num1 << 2; | |
repr("Original", num1); | |
repr("Result", res); | |
} | |
println!("====== num1 bitshift right by 1"); | |
{ | |
let res = num1 >> 1; | |
repr("Original", num1); | |
repr("Result", res); | |
} | |
println!("====== num1 bitshift right by 2"); | |
{ | |
let res = num1 >> 2; | |
repr("Original", num1); | |
repr("Result", res); | |
} | |
println!("====== num1 bitshift right by 4"); | |
{ | |
let res = num1 >> 4; | |
repr("Original", num1); | |
repr("Result", res); | |
} | |
println!("====== num2 bitshift left by 1"); | |
{ | |
let res = num2 << 1; | |
repr("Original", num2); | |
repr("Result", res); | |
} | |
println!("====== num2 bitshift left by 2"); | |
{ | |
let res = num2 << 2; | |
repr("Original", num2); | |
repr("Result", res); | |
} | |
println!("====== num2 bitshift right by 1"); | |
{ | |
let res = num2 >> 1; | |
repr("Original", num2); | |
repr("Result", res); | |
} | |
println!("====== num2 bitshift right by 2"); | |
{ | |
let res = num2 >> 2; | |
repr("Original", num2); | |
repr("Result", res); | |
} | |
println!("====== num2 bitshift right by 4"); | |
{ | |
let res = num2 >> 4; | |
repr("Original", num2); | |
repr("Result", res); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment