Created
October 25, 2011 21:12
-
-
Save cschamp/1314295 to your computer and use it in GitHub Desktop.
Find endianness
This file contains hidden or 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 <iostream> | |
using namespace std; | |
namespace { | |
bool A = false; | |
bool B = false; | |
struct C { | |
C() { | |
union u { | |
long l; | |
char c[sizeof(long)]; | |
} u; | |
u.l = 1; | |
if (u.c[0] == 1) { | |
B = true; | |
} | |
else { | |
A = true; | |
} | |
} | |
}; | |
C _c; | |
}; | |
int main() { | |
cout << "A: " << A << endl; | |
cout << "B: " << B << endl; | |
return 0; | |
} | |
// Output on my laptop is: | |
// A: 0 | |
// B: 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment