Created
August 15, 2015 06:24
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; } | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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