Skip to content

Instantly share code, notes, and snippets.

@IQAndreas
Created August 15, 2015 06:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IQAndreas/fc98032167ef1d8d4ae4 to your computer and use it in GitHub Desktop.
Save IQAndreas/fc98032167ef1d8d4ae4 to your computer and use it in GitHub Desktop.
How to tell if integers are stored as big or little endian (basically just look in the memory locations, but convert the pointer from an `*int` to a `*char` before incrementing)
#include <iostream>
using namespace std;
int main() {
int integers[5] = {4, 13, 14, 24, 256};
int *int0 = &integers[0];
char *c = (char*)int0;
char *end = (char*)&integers[5];
int i = 0;
while (c < end) {
cout << (int)(*c) << " ";
c++;
// Just add a newline every 4 bytes for clarity
if (++i % sizeof(int) == 0) { cout << endl; }
}
}
$ bin/endian
4 0 0 0
13 0 0 0
14 0 0 0
24 0 0 0
0 1 0 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment