Hacker News new | ask | show | jobs
by detaro 2791 days ago
which 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.
1 comments

Rob 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().