C Puzzles

yet another place for C puzzles

Monday, March 15, 2010

 

Big Endian or Little Endian

a c-program to find the endians of a processor?

answer:

unsigned int i = 0xabcdefgh;

unsigned char *pc = (unsigned char *) &i;

if (*pc == 0xgh) {
/* little endian */
} else {
/* big endian */
}

Comments:
https://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Data/endian.html
 
union foo {
unsigned short int i;
unsigned char a[2];
}

i=0xabcd;
print a[0];
 
1 #include
2
3 int main(){
4 int x = 1;
5 if ( *((char *)&x) == 1)
6 printf("Little endian\n");
7 else
8 printf("Big endian\n");
9 }
 
Post a Comment

Subscribe to Post Comments [Atom]





<< Home

Archives

July 2005   August 2005   October 2005   December 2005   March 2006   June 2006   July 2006   December 2006   February 2007   June 2007   March 2010   May 2010  

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]