Last active
June 11, 2018 14:51
-
-
Save Shidfar/561217fbd09e8273ebc45cf93ac9ea92 to your computer and use it in GitHub Desktop.
playing with strict aliasing
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 <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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please checkout the explanations here.