Last active
May 27, 2018 04:38
-
-
Save jaredscheib/08595d22f73a16622851f93390ec417a to your computer and use it in GitHub Desktop.
Determine if computer processor is using little endian or big endian
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int main () { | |
char x = 'x'; | |
char z = 'z'; | |
__int32_t w = x*(1<<24) + z; | |
__int32_t* p = &w; | |
char o = *(char*)p; | |
if (o == z) { | |
printf("processor is using little endian"); | |
} else { | |
printf("processor is using big endian"); | |
} | |
return 0; | |
} | |
// original, unused hardcoded test | |
int testEndianHardcoded() { | |
__int32_t x = 0x7800007a; | |
__int32_t* p = &x; | |
char y = *((char*)p+3); | |
printf("%i, %c", x, y); | |
return 0; | |
} | |
// Co-authored by Greg Little |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment