Saturday, March 23, 2013

Checking Endianness with C

The piece of code is written on April 23, 2010.
#include <stdio.h>
int checkEndian(){
 int p = 0xffff0000;
 if(*(char*)&p==0xff) //char* pointer of p's first byte
   printf("Big Endian");
   printf("Little Endian"); //my pc is little endian
 return 0;
int main(){


If the value of integer variable (32 bits) is 0xffff0000 and your system is in big endian, it will be stored as binary in memory like:
low addr:  1111 1111
       |   1111 1111
       ↓   0000 0000
high addr: 0000 0000

No comments:

Post a Comment