3 ms·
> Variable length encoding with a prepended length field The OP's method only works under the following assumptions: (1) length in bytes is power of 2 so the r
by Const-me 4y ago
> Variable length encoding with a prepended length field
The OP's method only works under the following assumptions: (1) length in bytes is power of 2 so the re-interpret trick works, and (2) the complete source data is in memory.
In many popular formats (MKV, WebM, etc) the header is variable-length as well e.g. 1-8 bits, the complete integer is therefore [ 1 .. 8 ] bytes, and because these files can be very large they don't fit in memory, need to stream from disk.
Under these conditions, the big-endian format (a) eliminates need for any bit shifts, only need to mask away the header in higher bits, slightly faster on most CPUs (b) preserves original bytes in the number except the MSB which contain the header, helps with debugging.
I think that's the only use case where big endian has a substantial advantage. Fortunately, all modern CPUs have fast instructions to flip the endianness, e.g. on Intel/AMD they are bswap for 4 or 8 bytes, ror/rol for 2 bytes, available as intrinsic or standard library functions in most programming languages.
For all other cases, including binary formats I design, I only use little-endian convention. Little-endian CPUs have won, and I don't like doing unnecessary work flipping these bytes. I'm lazy, also a boilerplate like that is a common source of bugs.
- ZephyrBlu 4y agoThis is completely unrelated to the contents of your comment, but did you use (a) to denominate the first item and then (2) to denominate the second in your third paragraph?!
- watersb 4y agoNot the poster, but my Hacker News comments are sometimes written on my phone. Small screen. Limited viewport. Primitive text. Wrestling mightily with the autocorrect vs. technical terminology... My brain working set often loses things like proper list enumeration. :-)
- Const-me 4y agoThanks, fixed.