Cast operator | ||||||||||||||
Problem: given int i, access a particular byte | big-endian | little-endian | ||||||||||||
int i = 0x1234abcd; | Address | Contents | ||||||||||||
Is this big-endian or little-endian? How can we tell? | 1000 | 12 | cd | |||||||||||
By definition, if we look at the bits in i, the leftmost bits are | 1001 | 34 | ab | |||||||||||
0001 | 0010 | (big- or little-endian) | 1002 | ab | 34 | |||||||||
Value is independent of byte order | 1003 | cd | 12 | |||||||||||
Another way to look at 4 bytes: | 1004 | |||||||||||||
char c[4]; | 1005 | |||||||||||||
What about | 1006 | |||||||||||||
c[0] = (char) i; | 1007 | |||||||||||||