9 ms·
Byte order is one of the great unnecessary historical fuck ups in computing. A similar one is that signedness of char is machine dependent. It's typically sign
by amelius 6y ago
Byte order is one of the great unnecessary historical fuck ups in computing.
A similar one is that signedness of char is machine dependent. It's typically signed on Intel and unsigned on ARM.
Sigh!
- bregma 6y agoAnd which is the correct byte ordering, pray tell?
- rwmj 6y agoBig Endian of course :-) However the one which has won is Little Endian. Even IBM admitted this when it switched the default in POWER 7 to little endian. s390x is the only significant architecture that is still big endian.
- bonzini 6y agoLittle endian has the advantage that you can read the low bits of data without having to adjust the address. So you can for example do long addition in memory order rather than having to go backwards, or (with an appropriate representation such as ULEB128) in one pass without knowing the size.
- js8 6y agoMaybe I am biased working on mainframes, but I would personally take big endian over little endian. The reason is when reading a hex dump, I can easily read the binary integers from left to right.
- bonzini 6y agoThat's the only thing that BE has over LE. But for example bitmaps in BE are a huge source of bugs, as readers and writers need to agree on the size to use for memory operations. "SIMD in a word" (e.g. doing strlen or strcmp with 32- or 64-bit memory accesses) might have mostly fallen out of fashion these days, but it's also more efficient in LE.
- kstenerud 6y agoBig endian is easier for humans to read when looking at a memory dump, but little endian has many useful features in binary encoding schemes due to the low byte being first. I used to like big endian more, but after deep investigation I now prefer little endian for any encoding schemes.
- bombcar 6y agoCouldn’t encoding systems be redone with emphasis on the high-order bits? Or is the assumption that the values are clustered in the low bits?
- amelius 6y agoI think the fundamental problems is that if you start a computation using N most significant bits and then incrementally add more bits, e.g. N+M bits total, then your first N bits might change as a result. E.g. decimal example: 1.00/1.00 = 1.00 1.000/1.001 = 0.999000999000... (adding one more bit changes the first bits of the outcome)
- kstenerud 6y agoYou can put emphasis on high order bits, but that makes decoding more complex. With little endian the decoder builds low to high, which is MUCH easier to deal with, especially on spillover. For example, with ULEB128 [1], you just read 7 bits at a time, going higher and higher up the value you're reconstituting. If the value grows too big and you need to spill over to the next (such as with big integer implementations), you just fill the last bits of the old value, then put the remainder bits in the next value and continue on. With a big endian encoding method (i.e. VLQ used in MIDI format), you start from the high bits and work your way down, which is fine until your value spills over. Because you only have the high bits decoded at the time of the spillover, you now have to start shifting bits along each of your already decoded big integer portions until you finally decode the lowest bit. This of course gets progressively slower as the bits and your big integer portions pile up. Encoding is easier too, since you don't need to check if for example a uint64 integer value can be encoded in 1, 2, 3, 4, 5, 6, 7 or 8 bits. Just encode the low 8 bits, shift the source right by 8, repeat, until the source value is 0. Then backtrack to the as-yet-blank encoded length field in your message and stuff in how many bytes you encoded. You just got the length calculation for free. Use a scheme where you only encode up to 60 bit values, place the length field in the low 4 bits, and Robert's your father's brother! For data that is right-heavy (i.e. the fully formed data always has real data on the right side and blank filler on the left - such as uint32 value 8 is actually 0x00000008), you want a little endian scheme. For data that is left-heavy, you want a big endian scheme. Since most of the data we deal with is right-heavy, little endian is the way to go. You can see how this has influenced my encoding design in [2] [3] [4]. [1] https://en.wikipedia.org/wiki/LEB128 https://en.wikipedia.org/wiki/LEB128 [2] https://github.com/kstenerud/concise-encoding/blob/master/cbe-specification.md https://github.com/kstenerud/concise-encoding/blob/master/cb... [3] https://github.com/kstenerud/compact-float/blob/master/compact-float-specification.md https://github.com/kstenerud/compact-float/blob/master/compa... [4] https://github.com/kstenerud/compact-time/blob/master/compact-time-specification.md https://github.com/kstenerud/compact-time/blob/master/compac...
- wongarsu 6y agoBig and little endian are named after the never-ending "holy" war in Gulliver's Travels over how to open eggs. So we were always of the opinion that it doesn't really matter. But I open my eggs on the little end
- CountHackulus 6y agoMiddle-endian is the only correct answer. It's a tradeoff between both little-endian and big-endian. The PDP-11 got it right.
- pantalaimon 6y agoThe good thing is that Big Endian is pretty much irrelevant these days. Of all the historically Big Endian architectures, s390x is indeed the only one left that has not switched to little endian.
- akvadrako 6y agoNetwork protocols still mostly use "Network Byte Order", i.e. big endian.
- lanstin 6y agoOr text. Or handled by generated code like protobuf.
- tssva 6y agoNetwork byte order is big endian so it is far from being pretty much irrelevant these days.
- BenoitEssiambre 6y agoAlso, this might be irrelevant at the cpu level, but within a byte, bits are usually displayed most significant bit first, so with little endian you end up with bit order: 7 6 5 4 3 2 1 0 15 14 13 12 11 10 9 8 instead of 15 to 0 This is because little endian is not how humans write numbers. For consistency with little endianness we would have to switch to writing "one hundred and twenty three" as 321
- LightMachine 6y agoExactly. This is so infuriating. Whoever let little-endian win made a huge disfavor for humanity.
- kstenerud 6y agoThe only benefit to big endian is that it's easier for humans to read in a hex dump. Little endian on the other hand has many tricks available to it for building encoding schemes that are efficient on the decoder side.
- amelius 6y agoBy the way, mathematicians also have their fuck ups: https://tauday.com/tau-manifesto https://tauday.com/tau-manifesto
- 8jy89hui 6y agoFor anyone curious or who is still attached to pi, here is a response to the tau manifesto: https://blog.wolfram.com/2015/06/28/2-pi-or-not-2-pi/ https://blog.wolfram.com/2015/06/28/2-pi-or-not-2-pi/
- ta_ca 6y agothe greatest of all is lisp not being the most mainstream language, and we can only blame the lisp companies for this fiasco. in an ideal world we all would be using a lisp with parametric polymorphism. from highest level abstractions to machine level, all in one language.
- ta_ca 6y agoi hope these downvotes are due to my failure at english or the comment being off-topic (or both). if not, can i just replace lisp with rust and be friends again?
- deleted 6y ago[deleted]
- mytailorisrich 6y agoI don't think it's a fuck up, rather I think it was unavoidable: Both ways are equally valid and when the time came to make the decision, some people decided one way, some people decided the other way.
- joppy 6y agoWhy is it an issue any more than say, order of fields in a struct is an issue? In one case you read bytes off the disk by doing ((b[0] << 8) | b[1]) (or equivalent), with the order reversed the other way around. Any application-level (say, not a compiler, debugger, etc) program should not even need to know the native byte order, it should only need to know the encoding that the file it’s trying to read used.
- zabzonk 6y ago> order of fields in a struct This is defined in C to be the order the fields are declared in.
- occamrazor 6y agoBut the padding rules between fields are a mess.