Is there a programmatic way to detect whether or not you are on a big-endian or little-endian architecture? I need to be able to write code that will execute on an Intel or PPC system and use exactly the same code (i.e. no conditional compilation).
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- What are the problems associated to Best First Sea
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
while there is no quick and standard way to determine it, this will output it:
Declare an int variable:
Now use char* pointers to various parts of it and check what is in those parts.
Depending on which one points to 0xFF byte now you can detect endianness. This requires sizeof( int ) > sizeof( char ), but it's definitely true for the discussed platforms.
Here's another C version. It defines a macro called
wicked_cast()
for inline type punning via C99 union literals and the non-standard__typeof__
operator.If integers are single-byte values, endianness makes no sense and a compile-time error will be generated.
untested, but in my mind, this should work? cause it'll be 0x01 on little endian, and 0x00 on big endian?
Please see this article:
How about this?