using_bitmap64/
using_bitmap64.rs

1use fixed_bitmaps::Bitmap64;
2
3fn main() {
4    // Creates an empty bitmap
5    let mut bitmap = Bitmap64::default();
6
7    // Bitmaps implement Display so you can view what the map looks like
8    println!("Default bitmap: {}", bitmap);
9
10    // Bitmaps also convert to their respective unsigned int versions and back again easily
11    println!("Value of bitmap: {}", bitmap.to_u64());
12
13    // Let's do the same as above, but actually setting the values in the bitmap to something
14    bitmap |= Bitmap64::from(101);
15
16    // Will show 0000000000000000000000000000000000000000000000000000000001100101
17    println!("Bitmap after OR-ing with 101: {}", bitmap);
18
19    // Set the 4th index (the 5th bit) to true. Can simply unwrap the result to remove the warning, as we know
20    // for certain that 4 < 63
21    bitmap.set(4, true).unwrap();
22
23    // Will show that 117 (101 + 2^4) is the value of the bitmap
24    println!("Bitmap value: {}", bitmap.to_u64());
25
26    // Will print out the error thrown when trying to set an index out of bounds
27    match bitmap.set(64, true) {
28        Ok(_) => println!("That wasn't meant to happen... something's up with my implementation!"),
29        Err(error) => {
30            println!("Yep, threw an error as expected. Error message is as follows:");
31            eprintln!("{}", error);
32        }
33    }
34
35    // Multiple ways to create a new bitmap
36    let _empty = Bitmap64::default();
37    let _full = Bitmap64::from(u64::MAX);
38
39    // Equivalent ways to create a bitmap with last bits 1001
40    let _bitmap = Bitmap64::from(9);
41    let _bitmap = Bitmap64::from(0b1001);
42
43    // Sets the 7th least significant bit when creating a new bitmap (indexing starts at 0)
44    let mut bitmap = Bitmap64::from_set(6).unwrap();
45
46    // Use the set() method to work with specific bits
47    bitmap.set(6, false).unwrap();
48    bitmap.set(42, true).unwrap();
49
50    // Use get() to know the value of a specific bit
51    println!("Bit at index 42: {}", bitmap.get(42).unwrap());
52
53    // Freely use boolean operators &, |, and ^
54    let bitmap1 = Bitmap64::from(0b1001);
55    let bitmap2 = Bitmap64::from(0b1010);
56
57    let _and = bitmap1 & bitmap2;
58    let _or = bitmap1 | bitmap2;
59    let _xor = bitmap1 ^ bitmap2;
60
61    // Aritmetic operators are currently used as exactly that, the following is guarunteed to continue working as it does
62    let _add = bitmap1 + 10;
63    let _sub = bitmap1 - 4;
64    let _mul = bitmap2 * 2;
65    let _div = bitmap2 / 2;
66
67    // The following works exactly as above, but is likely to change in favour of set operations in the major update to 1.0.0
68    let _add = bitmap1 + Bitmap64::from(10);
69    let _sub = bitmap1 - Bitmap64::from(4);
70    let _mul = bitmap2 * Bitmap64::from(2);
71    let _div = bitmap2 / Bitmap64::from(2);
72}