dzamlo / rust-bitfield

This crate provides macros to generate bitfield-like struct.
Apache License 2.0
157 stars 19 forks source link

About endianness #24

Closed hug-dev closed 4 years ago

hug-dev commented 4 years ago

Hi!

Thank you for your fantastic crate 😃 I have a question: is this crate agnostic to endianness? Say if I map a field to bits 0 to 7 of a u32 value, will I get the same result if my CPU is little-endian or big-endian?

I was thinking that if you implemented this crate methods using bitwise operators and those operators are endianness agnostic in Rust (like in C AFAIK) then maybe your crate is as well?

I started by implementing two bitfields structure one for little-endian and one for big-endian and selecting the good one with cfg but maybe that is useless 😀

dzamlo commented 4 years ago

The generated code use butwise operation. The result should be the same regardless of endianess.

But endianess may play a role before this crate is involved. To convert bytes to a u32 (or array of u32) for example.

Playing with the bits_position exemple may be a god idea to understand that better.

hug-dev commented 4 years ago

Great! Thanks for the confirmation and the pointer. I only use u32 so should be fine 👍