4 ms·
That post by Rob Pike is arguing that you should not need to check the endianness of your system when swapping. Not that you should never byte swap. Systems and
by filam 8y ago
That post by Rob Pike is arguing that you should not need to check the endianness of your system when swapping. Not that you should never byte swap. Systems and protocols have different endianness, which needs to be handled by programs where endianness has not been abstracted away.
- detaro 8y agowhich is the same the submission says? Extract the data using a function that produces a value "mathematically" that's always correct for the host machine. His example uses multiplication instead of shifts like Pike, but that's a minor stylistic difference.
- LukeShu 8y agoRob Pike is saying that your code should never care/check what the host byte order is; if you use the libc FOOtoh() functions, you aren't caring what the host byte order is, you're just converting something to it. Put another way, Pike is saying that libc should have implemented be16toh() like: # define be16toh(x) ( (((char*)x)[1]<<0) | (((char*)x)[0]<<8) ) instead of how GNU libc implemented it: # if __BYTE_ORDER == __LITTLE_ENDIAN # define be16toh(x) __bswap_16 (x) # else # define be16toh(x) __uint16_identity (x) # endif But that complaint doesn't affect programs that call be16toh().