Skip to content

Odd load_le behavior #294

@bbaldino

Description

@bbaldino

I'm seeing some strange behavior when loading a little-endian value from a bitslice, and I'm wondering if I'm missing something. I've got the following two test scenarios:

use bitvec::{bits, field::BitField, order::Msb0, view::BitView};

fn test_data_is_offset() {
    #[rustfmt::skip]
    let data = bits![u8, Msb0;
        1, 0, 1, 0, 1, 0, 1, 1, 1, // 9 bits
        1, 1, 1, 1, 0, 0, 0, 0, 1, 1, // 10 bits
    ];

    println!("loading le from {:?}", &data[9..19]);
    let value: u16 = data[9..19].load_le();
    println!("value: {value}");
}

fn test_data_at_start() {
    let data = bits![u8, Msb0;
        1, 1, 1, 1, 0, 0, 0, 0, 1, 1, // 10 bits
    ];
    println!("loading le from {:?}", &data[0..10]);
    let value: u16 = data[0..10].load_le();
    println!("value: {value}");
}

fn main() {
    test_data_is_offset();
    test_data_at_start();
}

Both are trying to load a little-endian-encoded 10 bit slice into a u16. The first test loads those 10 bits from an offset 9 bits into the buffer and the second test loads the 10 bits from the start. The output of this test is as follows:

loading le from BitSlice<u8, bitvec::order::Msb0> { addr: 0x7ffee3ab8223, head: 001, bits: 10 } [1, 1, 1, 1, 0, 0, 0, 0, 1, 1]
value: 504
loading le from BitSlice<u8, bitvec::order::Msb0> { addr: 0x7ffee3ab8224, head: 000, bits: 10 } [1, 1, 1, 1, 0, 0, 0, 0, 1, 1]
value: 1008

So the slices they're reading from look exactly the same (save for the head value), but they come up with different results. If the 'offset' of the data for the first test is 8 bits instead of 9, then they both return the same result.

Is this a bug?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions