bitvec icon indicating copy to clipboard operation
bitvec copied to clipboard

BitVec -> BitSlice -> Slice

Open SetheenDev opened this issue 3 years ago • 1 comments

Let's say I'm storing two values in a BitVec. The first value is a single bit (U1), the second value is a 2-byte array. This leaves me with 17 bits spread across 3 bytes. To retrieve the first bit, I can use something like bitvec[0..1].load::<u8>(). How do I retrieve the next two bytes as a raw slice?

My first guess was &bitvec.as_raw_slice()[1..17] but this appears to index at the byte level, not the bit level. It pulls the wrong range. My second guess was &bitvec[1..17].as_raw_slice() but bitslices do not have method as_raw_slice(). Any pointers appreciated. I'm loving this library so far.

SetheenDev avatar Aug 10 '22 13:08 SetheenDev

BitSlice::domain (and briefly discussed in the guide)

Because BitSlice has to assume that its edge elements have aliasing conditions, it can't unconditionally create a [T] view. Instead, you need to route through the .domain() call and match on the fragments it gives you.

You can also use bitvec[1..17].load::<u16>() from the BitField trait.

BE WARNED THAT THIS CAN BE WEIRD

If you are going to use BitField to access memory, you must read this chapter of the guide and all of the documentation in the bitvec::field module. I am not kidding.

myrrlyn avatar Sep 04 '22 05:09 myrrlyn