Skip to content

Instantly share code, notes, and snippets.

@Shidfar
Last active June 11, 2018 14:51
Show Gist options
  • Save Shidfar/561217fbd09e8273ebc45cf93ac9ea92 to your computer and use it in GitHub Desktop.
Save Shidfar/561217fbd09e8273ebc45cf93ac9ea92 to your computer and use it in GitHub Desktop.
playing with strict aliasing
#include <stdio.h>
typedef unsigned int DWORD;
typedef unsigned short WORD;
inline DWORD
SwapWords (DWORD val)
{
WORD * p = (WORD*) &val;
WORD t;
printf("%d + (%d * 2^16)\n", p[0], p[1]);
t = p[0];
p[0] = p[1];
p[1] = t;
printf("DWORD: %d WORD: %d\n", sizeof(DWORD)*8, sizeof(WORD)*8);
printf("%d + (%d * 2^16)\n", p[0], p[1]);
return val;
}
int
main ()
{
printf ("%d\n", SwapWords (65539));
return 0;
}
@Shidfar
Copy link
Author

Shidfar commented Jun 11, 2018

Please checkout the explanations here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment