rustbitvector

In bitvec, how does storage size affect loading?


How does the storage size affect how loading works?.

As an example, in the below snippet, a 22 bit vector is separated into two groups of 11 bits. Each of these groups has the same pattern, storing the decimal number 1027.

    let vec22 = bitvec![u8 /* usize */, Msb0; 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, /* next */ 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1]; // 1027, 1027
    let chunks = vec22.chunks(11);

    let numbers = chunks
        .into_iter()
        .map(|chunk| {
            let number: usize = chunk.load();
            println!("{} {}", chunk, number);
            number
        })
        .collect::<Vec<_>>();

When the storage size is usize, the load method returns the expected 1027. When set to u8, I can't seem to find a relationship between the printed numbers and the bit patterns,

[1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1] 896  // expected 1027
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1] 112  // expected 1027

How does changing the storage size affect these numbers?


Solution

  • There is some fundamental knowledge on endianess and bitvec that's necessary to be able to answer the question,

    Finally, to answer OP question as to why switching between u8 and usize was producing different results: because uisng u8 caused the 11-bit chunk to be spread across 2 bytes, thus making bitvec choose how to interpret the relative significance of the bits across those bytes. It just so happens that load, which uses the endianness of the machine it's running on, didn't match the intended endianness with which vec22 is defined.