Skip to content

Instantly share code, notes, and snippets.

@cschamp
Created October 25, 2011 21:12
Show Gist options
  • Save cschamp/1314295 to your computer and use it in GitHub Desktop.
Save cschamp/1314295 to your computer and use it in GitHub Desktop.
Find endianness
#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