I wrote this program for endian conversion and in my current machine which is little endian it works fine. Is there some better logic to do the same or is this okay (i find it pretty confusing though :-)).

[insert]Code:#include <stdio.h> #include <stdlib.h> void showbits(unsigned int i); int main(void){ unsigned int i = 543295629, j, result = 0, temp,final = 0, k; showbits(i); for(j = 31; j > 0; j--){ temp = i; k = temp >> j; k = k & 1; if (k == 1){ final = final; result = result >> (31-j); result = result | k; result = result & 1; result = result << (31-j); final = final| result; //showbits(result); } else{ final = final; result = result >> (31-j); result = result | k; result = result & 1; result = result << (31-j); final = final| result; //showbits(result); } } // do it for the last bit as well final = final; temp = i; temp = temp & 1; result = result >> 31; result = result | temp; result = result & 1; result = result << 31; final = final| result; //showbits(result); printf("\n After conversion is done Big endian to Little endian we have"); showbits(final); return 0; } void showbits(unsigned int i){ unsigned int u; printf("\n"); for(u = (~0U >> 1) + 1; u > 0; u = u>>1){ putchar(i & u? '1' : '0'); } putchar('\n'); }