5 ms·
These days I think the sane option is to just add a static assert that the machine is little endian and move on with your life. Unless you're writing glibc or s
by IshKebab 2y ago
These days I think the sane option is to just add a static assert that the machine is little endian and move on with your life. Unless you're writing glibc or something do you really care about supporting ancient IBM mainframes?
Also recommending octal is sadistic!
- bvrmn 2y ago> add a static assert that the machine is little endian and move on with your life It's not clear how it would free you from interpreting BE data from incoming streams/blobs.
- forrestthewoods 2y agoI feel like we're at a point where you should assume little endian serialization and treat anything big endian as a slow path you don't care about. There's no real reason for any blob, stream, or socket to use big endian for anything afaict. If some legacy system still serializes big endian data then call bswap and call it a day.
- bvrmn 2y agoAFAIK quite a number of protocols and file formats use BE without any sign to become a legacy even in a distant future.
- saagarjha 2y agoYou do realize that most of the networking stack is big-endian, right?
- syncsynchalt 2y agoThe internet is big-endian, and generally data sent over the wire is converted to/from BE. For example the numbers in IP or TCP headers are big-endian, and any RFC that defines a protocol including binary data will generally go with big-endian numbers. I believe this dates from Bolt Baranek and Newman basing the IMP on a BE architecture. Similarly computers tend to be LE these days because that's what the "winning" PC architecture (x86) uses.
- userbinator 2y agoOnly the early protocols below the application layer are BE. A lot of the later stuff switched to LE.
- kortilla 2y agoYes, those “early protocols” carry everything. Until applications stop opening sockets, this problem doesn’t go away.
- lmm 2y agoIf you're writing an implementation of one of those "early protocols", sure. If not, call a well-known library, let it do whatever bit twiddling it needs to, and get on with what you were actually doing.
- 01HNNWZ0MV43FF 2y agoBut the payload isn't BE
- forrestthewoods 2y ago> any RFC that defines a protocol including binary data will generally go with big-endian numbers I'm not sure this is true. And if it is true it really shouldn't be. There are effectively no modern big endian CPUs. If designing a new protocol there is, afaict, zero benefit to serializing anything as big endian. It's unfortunate that TCP headers and networking are big endian. It's a historical artifact. Converting data to/from BE is a waste. I've designed and implemented a variety of simple communication protocols. They all define the wire format to be LE. Works great, zero issues, zero regrets.
- classichasclass 2y ago> There are effectively no modern big endian CPUs. POWER9, Power10 and s390x/Telum/etc. all say hi. The first two in particular have a little endian mode and most Linuces run them little, but they all can run big, and on z/OS, AIX and IBM i, must do so. I imagine you'll say effectively no one cares about them, but they do exist, are used in shipping systems you can buy today, and are fully supported.
- bla3 2y agohttps://commandcenter.blogspot.com/2012/04/byte-order-fallacy.html https://commandcenter.blogspot.com/2012/04/byte-order-fallac... covers that part.
- MenhirMike 2y agoBig Endian is also called Network Order because some networking protocols use it. And of course, UTF-16 BE is a thing. There is a non-trivial chance that you will have to deal with BE data regardless if your machine is LE or BE.
- foobarian 2y ago> some networking protocols Pretty low-key way to refer to pretty much all layer 1-3 IETF protocols :D
- MenhirMike 2y agoIt's like Tim Berners-Lee being referred to as "Web Developer" :) https://imgur.com/kX5oBk6 https://imgur.com/kX5oBk6
- IshKebab 2y agoYeah but that's a known order. You don't have to detect it.
- circuit10 2y agoI write programs for my calculator, which is big endian
- IshKebab 2y agoGood for you I guess?
- circuit10 2y agoIt’s an example of somewhere big endian is used other than an IBM mainframe, even if it’s equally niche I’m pretty sure there are examples that are way less niche though but I don’t want to look into it at the moment
- rerdavies 2y agoWhich calculator? And does it have a C compiler?
- circuit10 2y agoIt’s the Casio CG50, it uses SuperH which is supported by GCC and there is an unofficial SDK (well there are actually two of them) The CPU is technically bi-endian but it’s controlled by a pin and it’s hardwired to big endian mode Most C code just works, sometimes there are endianness bugs when porting things but they’re usually not hard to fix
- zajio1am 2y agoBE MIPS is still alive, many recent Mikrotik hardware is BE MIPS.
- Gibbon1 2y agoThat should just be your bad decision shouldn't be other people problems.
- bewo001 2y agoThe fun really starts if you have a CPU using big endian and a bus using little endian.. Back in the late 1990s, we moved from Motorola 68k / sbus to Power/PCI. To make the transition easy, we kept using big endian for the CPU. However, all available networking chips only supported PCI / little endian at this point. For DMA descriptor addresses and chip registers, one had to remember to use little endian.
- m463 2y agoor older macs (but mostly, network byte order is big endian)
- matheusmoreira 2y agoPlenty of binary formats out there containing big endian integers. An example is the format I'm dealing with right now: ELF files. Their endianness matches that of the ELF's target architecture. Apparently they designed it that way in order to ensure all values in the ELF are naturally encoded in memory when it is processed on the architecture it is intended to run on. So if you're writing a program loader or something you can just directly read the integers out of the ELF's data structures and call it a day. Processing arbitrary ELF inputs requires adapting to their endianness though.