bitvec icon indicating copy to clipboard operation
bitvec copied to clipboard

Bug (?): load() loads wrong value

Open Schievel1 opened this issue 2 years ago • 0 comments

Hello,

I'm maybe wrong here and this is expected behavior, but when I load a byte from a slice with the indices [2..10] from a parent array, I get the bits [0..8] back.

e.g.:

use bitvec::prelude::*;

fn main() {
    let buf: [u8; 2] = [0b01011111, 0b01101100];
    let bits = buf.view_bits::<Msb0>();
    print!("buf: {:08b}",&buf[0]);
    println!(" {:08b}",&buf[1]);
    println!("direct: {:08b}", &bits[2..10]);
    println!("via load 2..10: {:08b}", bits[2..10].load::<u8>());
    println!("direct: {:08b}", &bits[6..14]);
    println!("via load 6..14: {:08b}", bits[6..14].load::<u8>());
}

Output:

buf: 01011111 01101100
direct: [011111  , 01      ]
via load 2..10: 01011111
direct: [11      , 011011  ]
via load 6..14: 01101111

I know this probably has something to do with the Msb0 ordering of the Bitslice. But I don't understand how it mangles a slice that is exactly the size of a byte that much. It is putting the bits from the second byte it finds first, then puts the bits from the first bind behind them.

How can I just get the slice and make an u8 from it, regardless of what the byte boundaries of the parent array are?

Schievel1 avatar Mar 09 '23 14:03 Schievel1