J
Joe Laughlin
#include <stdio.h>
int main()
{
unsigned long num;
unsigned int a = 10, b = 20, c = 30, d = 40;
/* num = 0; */
num |= a << 24;
num |= b << 16;
num |= c << 8;
num |= d << 0;
printf("a = %02.2x\nb = %02.2x\nc = %02.2x\nd = %02.2x\n", a, b, c, d);
printf("num = %08.8x\n", num);
return 0;
}
What I'm trying to do here is pack a, b, c, and d into num.
It works if I set num = 0, but how come it's not working if I leave it out
(like above)? I know that when num is declared, its memory contents is full
of garbage, but I thought that by packing the ints into it would've
overwritten all the garbage? (hope that makes sense)
Thanks,
Joe
int main()
{
unsigned long num;
unsigned int a = 10, b = 20, c = 30, d = 40;
/* num = 0; */
num |= a << 24;
num |= b << 16;
num |= c << 8;
num |= d << 0;
printf("a = %02.2x\nb = %02.2x\nc = %02.2x\nd = %02.2x\n", a, b, c, d);
printf("num = %08.8x\n", num);
return 0;
}
What I'm trying to do here is pack a, b, c, and d into num.
It works if I set num = 0, but how come it's not working if I leave it out
(like above)? I know that when num is declared, its memory contents is full
of garbage, but I thought that by packing the ints into it would've
overwritten all the garbage? (hope that makes sense)
Thanks,
Joe