Hi, I am trying to follow a book example on referencing union members and the method below has been used without explanation. Could anyone explain please?
union u {
char ch[2];
int num;
};
int unioninit (union u val); /*declare function to return int*/
main()
{
union u val;
int x;
x = unioninit(val) /*this assigns values two chars to the ch array*/
printf("the two characters held by the union are:\n");
printf("%c\n", x & 0x00FF); /* this is my problem */
printf("%c\n", x>>8);
.............
What I don't understand is how comparing x and 0x00FF has the effect of displaying the first byte of the int num variable.
If you don't include the bitwise operator surely it does the same since the %c only asks for the first byte???
And where did that value 00FF come from anyway?
Can anyone help?
Bukko