IME, there's one big thing that often keeps my programs from being unaffected by byte order: wanting to quickly splat data structures into and out of files, pipes, and sockets, without having to encode or decode each element one-by-one. The only real way to make this endian-independent is to have byte-swapping accessors for everything when it's ultimately produced or consumed, but adding all the code for that is very tedious in most languages. One can argue that handling endianness is the responsible thing to do, but it just doesn't seem worthwhile when I practially know that no one will ever run my code on a big-endian processor.
If you are using C/C++ for any new app, there is a possibility you are writing code that has a performance requirement.<p>- mmap/io_uring/drivers and additional "zero-copy" code implementations require consideration about byte order.<p>- filesystems, databases, network applications can be high throughput and will certainly benefit from being zero-copy (with benefits anywhere from +1% to +2000% in performance.)<p>This is absolutely not "premature optimization." If you're a C/C++ engineer, you should know off the top of your head how many cycles syscalls & memcpys cost. (Spoiler: They're <i>slow</i>.) You should evaluate your performance requirements and decide if you need to eliminate that overhead. For certain applications, if you do not meet the performance requirements, you cannot ship.
TCP/IP is big-endian, which is likely the largest footprint for these concerns.<p>"htonl, htons, ntohl, ntohs - convert values between host and network byte order"<p>The cheapest big-endian modern device is a Raspberry Pi running a NetBSD "eb" release, for those who want to test their code.<p><a href="https://wiki.netbsd.org/ports/evbarm/" rel="nofollow">https://wiki.netbsd.org/ports/evbarm/</a>
Unless you're dealing with binary data in which case byte order matters very much and if you forget to convert it you're causing a world of pain for someone.<p>He even has an example where he just pushes the problem off to someone else <i>"if the people at Adobe wrote proper code to encode and decode their files"</i>, yeah hope they weren't ignoring byte order issues.
Really except for the networking (including say Bluetooth) nobody is big endian anymore. So how about just don't leak that thing from the network layer.<p>And do not define any data format to be big endian anymore. Deine it as little endian (do <i>not</i> leave it undefined) and everyone will be happy.
This is a reasonable way to do things, and I've used it before. However I just used Zig's method here, and like it a lot: <a href="https://ziglang.org/documentation/master/std/#std.io.Reader.readInt" rel="nofollow">https://ziglang.org/documentation/master/std/#std.io.Reader....</a><p>Given a reader (file, network, buffers can all be turned into readers), you can call readInt. It takes the type you want, and the endianess of the encoding. It's easy to write, self documents, and it's highly efficient.
As a games coder I was glad when the xbox 360 / ps3 era came to an end; getting big endian clients talking to little endian servers was an endless source of bugs.
The other case where it matters is SIMD instructions where you're serializing or deserializing multiple fields at once, but the SIMD operations are usually architecture specific to begin with and so if you shuffle bytes into and out of the native packed formats it will be specific to the endianness of the native packed format, and then you can forget about byte order outside of those shuffle transformations.
What he said: if you read bytes with some byte order, you compose them yourself correctly, no byte swapping but just reading byte for byte and convert them to the number value you need. The architecture byte order is implicit as long as you use the architecture's tools to convert the bytes.<p>Rust, for example has from_be_bytes(), from_le_bytes() and from_ne_bytes() methods for the number primitives u16, i16, u32, and so on. They all take a byte array of the correct length and interpret them as big, little and native endian and convert them to the number.<p>The first two methods work fine on all architectures, and that's what this article is about.<p>The third method, however, is architecture-dependent and should not be used for network data, because it would work differently and that's what you don't want. In fact, let me cite this part from the documentation. It's very polite but true.<p>> As the target platform’s native endianness is used, portable code likely wants to use from_be_bytes or from_le_bytes, as appropriate instead.
I don't like these ambiguous titles. From the title I thought I was going to read that byte order doesn't matter when in fact the title should be "a computer's byte order is irrelevant to high-level languages". At least, state the fallacy in unambiguous terms one sentence right away. In any case, was an interesting read.
> If you wrote it on a PC and tried to read it on a Mac, though, it wouldn't work unless back on the PC you checked a button that said you wanted the file to be readable on a Mac. (Why wouldn't you? Seriously, why wouldn't you?)<p>As a non-SWE, whenever I see checkboxes to enable options that maximize compatibility, I often assume there’s an implicit trade-off, so if it isn’t checked by default, I don’t enable such things unless strictly necessary. I don’t have any solid reason for this, it’s just my intuition. After all, if there were no good reasons <i>not</i> to enable Mac compatibility, why wouldn’t it be the default?<p>Edit: spelling error with “implicit”
Be aware that if you actually want to do as the article prescribes, don't just copy and paste -- you shan't take anything at face value in C: <a href="https://news.ycombinator.com/item?id=31718292">https://news.ycombinator.com/item?id=31718292</a>.
He's right that you shouldn't use ifdefs, but I think a macro like le32toh() is far clearer and more concise than a bunch of shifts and ors.<p>Also, a lot of comments in this thread have nothing to do with the article and appear to be responses to some invisible strawman.
The byte order matters in all cases where there is i/o, being files, network streams, inter chip communication,... For data that stays on the same processor or for files that are only accessed with the processors of the same endianness, there really is no issue, even when doing bit manipulation.