Skip to content

Instantly share code, notes, and snippets.

@caigen
Created October 12, 2014 15:44
Show Gist options
  • Save caigen/e90f33b18f65bf8e81ec to your computer and use it in GitHub Desktop.
Save caigen/e90f33b18f65bf8e81ec to your computer and use it in GitHub Desktop.
32bit-64bit small-endian big-endian
#include <iostream>
int main(int argc, char* argv[]) {
std::cout << sizeof(void *) << std::endl;
std::cout << sizeof(int *) << std::endl;
std::cout << sizeof(double *) << std::endl;
int* p = 0x0;
p++;
if (p == (int *)0x4) {
std::cout << "32 bit" << std::endl;
}
// 0x01 0x02 0x03 0x04
//
union {
int i;
char c;
} x;
x.i = 0x87654321;
if (x.c == 0x87) {
std::cout << "big endian" << std::endl;
}
else if (x.c == 0x21) {
std::cout << "small endian" << std::endl;
}
int xx = 0x87654321;
char c = *(char *)&xx;
std::cout << (int)c << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment