Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug (?): load() loads wrong value #219

Open
Schievel1 opened this issue Mar 9, 2023 · 0 comments
Open

Bug (?): load() loads wrong value #219

Schievel1 opened this issue Mar 9, 2023 · 0 comments

Comments

@Schievel1
Copy link

Schievel1 commented Mar 9, 2023

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 added a commit to Schievel1/mview that referenced this issue Mar 9, 2023
when the bytes were not read from byte boundaries in the buff but from
across them.

See also: ferrilab/bitvec#219

Signed-off-by: Pascal Jäger <pascal.jaeger@leimstift.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant