From 0df54ca55134c4240c43fd7db50d665e0c32ccb3 Mon Sep 17 00:00:00 2001 From: Kohei Yoshida Date: Mon, 23 Aug 2010 10:49:05 -0400 Subject: Slightly more elegant solution using union. --- check-endian.cpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/check-endian.cpp b/check-endian.cpp index f355485..c4bd6f2 100644 --- a/check-endian.cpp +++ b/check-endian.cpp @@ -4,14 +4,19 @@ using namespace std; +union endian_check +{ + int number; + char check; +}; + int main(int argc, char** argv) { - int num = 1; - char* p = reinterpret_cast(&num); - cout << "size of int: " << sizeof(num) << endl; + endian_check _check; + _check.number = 1; + // Check the valu of the least significant byte. - int check = static_cast(p[0]); - if (check) + if (_check.check) cout << "little endian" << endl; else cout << "big endian" << endl; -- cgit v1.2.3