Skip to content

Instantly share code, notes, and snippets.

@markd2
Created August 30, 2012 18:10
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save markd2/3536046 to your computer and use it in GitHub Desktop.
Save markd2/3536046 to your computer and use it in GitHub Desktop.
Explore endianness.
#import <Foundation/Foundation.h>
// Xcode_3_2_6/usr/bin/gcc -arch ppc -std=c99 -isysroot /Xcode_3_2_6/SDKs/MacOSX10.5.sdk -g -Wall -framework Foundation -o endian-ppc ~/Downloads/endian.m (thanks to Jeremy W. Sherman for the assist)
// clang -arch x86_64 -g -Weverything -framework Foundation -o endian endian.m
int main (void) {
unsigned int values[] = { 1, 387, 8533937 };
printf ("scan by ints\n");
for (unsigned int i = 0; i < sizeof(values) / sizeof(*values); i++) {
printf (" 0x%08x", values[i]);
}
printf ("\n");
printf ("scan by bytes\n");
for (unsigned int i = 0; i < sizeof(values); i++) {
if (i % 4 == 0) printf (" 0x");
printf ("%02x", ((unsigned char *)values)[i]);
}
printf ("\n");
return 0;
} // main
#if 0
Outputs:
scan by ints
0x00000001 0x00000183 0x008237b1
scan by bytes
0x01000000 0x83010000 0xb1378200
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment