Skip to content

Unaligned load_le / load_be return incorrect values #300

Description

@Finomnis

Slices should return the same load_be/load_le values regardless of whether they are aligned to bytes (at least that's what I assume)

But currently, they don't:

use bitvec::prelude::*;

fn main() {
    println!("");
    println!("--- Byte-Aligned ---");

    let bv = bits![u8, Msb0; 0, 0, 0, 0, 1, 1, 1, 1];
    println!("Msb0+load_be: {} -> {:08b}", bv, bv.load_be::<u8>());

    let bv = bits![u8, Msb0; 0, 0, 0, 0, 1, 1, 1, 1];
    println!("Msb0+load_le: {} -> {:08b}", bv, bv.load_le::<u8>());

    let bv = bits![u8, Lsb0; 0, 0, 0, 0, 1, 1, 1, 1];
    println!("Lsb0+load_be: {} -> {:08b}", bv, bv.load_be::<u8>());

    let bv = bits![u8, Lsb0; 0, 0, 0, 0, 1, 1, 1, 1];
    println!("Lsb0+load_le: {} -> {:08b}", bv, bv.load_le::<u8>());

    println!();
    println!("--- Unaligned ---");

    let bv = bits![u8, Msb0; 0, 0, 0, 0, 0, 1, 1, 1, 1];
    let bv = &bv[1..];
    println!("Msb0+load_be: {} -> {:08b}", bv, bv.load_be::<u8>());

    let bv = bits![u8, Msb0; 0, 0, 0, 0, 0, 1, 1, 1, 1];
    let bv = &bv[1..];
    println!("Msb0+load_le: {} -> {:08b} (!)", bv, bv.load_le::<u8>());

    let bv = bits![u8, Lsb0; 0, 0, 0, 0, 0, 1, 1, 1, 1];
    let bv = &bv[1..];
    println!("Lsb0+load_be: {} -> {:08b} (!)", bv, bv.load_be::<u8>());

    let bv = bits![u8, Lsb0; 0, 0, 0, 0, 0, 1, 1, 1, 1];
    let bv = &bv[1..];
    println!("Lsb0+load_le: {} -> {:08b}", bv, bv.load_le::<u8>());

    println!();
}
--- Byte-Aligned ---
Msb0+load_be: [0, 0, 0, 0, 1, 1, 1, 1] -> 00001111
Msb0+load_le: [0, 0, 0, 0, 1, 1, 1, 1] -> 00001111
Lsb0+load_be: [0, 0, 0, 0, 1, 1, 1, 1] -> 11110000
Lsb0+load_le: [0, 0, 0, 0, 1, 1, 1, 1] -> 11110000

--- Unaligned ---
Msb0+load_be: [0, 0, 0, 0, 1, 1, 1, 1] -> 00001111
Msb0+load_le: [0, 0, 0, 0, 1, 1, 1, 1] -> 10000111 (!)
Lsb0+load_be: [0, 0, 0, 0, 1, 1, 1, 1] -> 11100001 (!)
Lsb0+load_le: [0, 0, 0, 0, 1, 1, 1, 1] -> 11110000

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