#include <stdio.h>
int main() {
/* Little endian Always remember LL0 (little lsb stores in 0 location)
If the var prints as it on the console i.e 0x11223344 on console , it is little endian
LSB byte gets stored in 0x0 .
44 -> ptr[0]
33 -> ptr[1]
22 -> ptr[2]
00 -> ptr[3]
Big Endian
00 -> ptr[0]
11 -> ptr[1]
22 -> ptr[2]
33 -> ptr[3]
It prints like 0x11223344 . Only when you break into single byte , char ptr will be pointing to MSB i.e 11
*/
int var = 0x11223344;
printf("var %x\n",var);
char *ptr = &var;
int i=0;
for(i=0;i<4;i++)
printf("val %x %i\n",ptr[i],i);
//MACRO TO FIND LITTLE ENDIAN OR BIG ENDIAN
if(ptr[0] == 0x44){
printf("Little Endian\n");
}else{
printf("Big Endian\n");
}
//BitWise
printf("(var << 1) %d\n",(var & (1<<0)));
printf("%s\n",((var << 1)==0) ? "Little Endian" : "Big Endian" );
}
return 0;
}
To embed this program on your website, copy the following code and paste it into your website's HTML: