summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKohei Yoshida <kyoshida@novell.com>2010-08-23 10:49:05 -0400
committerKohei Yoshida <kyoshida@novell.com>2010-08-23 10:49:05 -0400
commit0df54ca55134c4240c43fd7db50d665e0c32ccb3 (patch)
tree267983f4c1f24518f0eff6e84e5ee683c65f58df
parent8ac12eb4a36da290817240edd62cb69f7eab81fd (diff)
Slightly more elegant solution using union.
-rw-r--r--check-endian.cpp15
1 files 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<char*>(&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<int>(p[0]);
- if (check)
+ if (_check.check)
cout << "little endian" << endl;
else
cout << "big endian" << endl;