Saturday, 15 March 2014

c - forcing a byte order -



c - forcing a byte order -

unsigned char endian[2] = {1, 0}; short x; x = *(short *) endian;

i don't understand value held in x. if have little-endian system, x 1.

but if have big-endian system, x 256.

why value 256 rather 128 ?

thanks help.

because bytes 01 00, in big-endian, value 0x0100, equals 1 << 8 == 2**8 == 256 (recall there 8 bits in byte).

c byte endianness

No comments:

Post a Comment