Determine whether the machine is big-endian or little-endian


Union union and big-endian (big-endian, little-endian):
The following demonstrates a use, representing four variables with four meanings, but can be operated with an int, direct int assignment, regardless of memory access (pointer size The integer multiple of , access is efficient),
or the time complexity (the difference between one and four times, and three of these four times are uneven addresses), will be lower.



1
#include<stdio.h> 2 union var { 3 char c[ 4 ]; 4 int i; 5 }; 6 7 int main(){ 8 union var data; 9 data.c[ 0 ] = 0x04 ; // Because it is a char type, the number should not be too large. Calculate the range of ascii ~ 10 data.c[ 1 ] =0x03 ; // Written in hexadecimal for the convenience of directly printing the value in the memory for comparison 11 data.c[ 2 ] = 0x02 ; 12 data.c[ 3 ] = 0x11 ; 13 // The lower subscript in the array has a lower address , according to the address from low to high, the memory contents are: 04, 03, 02, 11. Four bytes in total! 14 // And take the four bytes as a whole (regardless of type, directly print hexadecimal), it should be seen from the high address of the memory to the low address, 0x11020304, and the low 04 is placed on the low address. 15 printf( " %x\n " ,data.i); 16 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324891504&siteId=291194637