Skip to content

Instantly share code, notes, and snippets.

@Tricky1975
Created August 23, 2020 21:27
Show Gist options
  • Save Tricky1975/3fe3ab2571011a16896ceaf0c23ddb49 to your computer and use it in GitHub Desktop.
Save Tricky1975/3fe3ab2571011a16896ceaf0c23ddb49 to your computer and use it in GitHub Desktop.
Just a little routine I whipped up to check if the system the code runs on is LittleEndian or not (now there ain't much BigEndian systems anymore as far as I know, but they are still there, I guess).
#define Test_EndianCheck
typedef union {
unsigned char b[4];
int i;
} ucheck;
namespace TrickyUnits{
bool IsLittleEndian(){
ucheck chk;
chk.i=256;
return chk.b[1]==1;
}
bool IsBigEndian() { return !IsLittleEndian();}
}
#ifdef Test_EndianCheck
#include <iostream>
using namespace TrickyUnits;
int main(){
if (IsLittleEndian())
std::cout << "System is LittleEndian\n";
else
std::cout << "System is BigEndian\n";
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment