|You are right. The data type that I am shifting however is an unsigned int and I think that is what is causing my confusion. I guess that in c++ different data types overload the shift >> operator differently. For instance since an unsigned int has no sign then |
unsigned int a = 0xFFFFFFFF;
a = (a >> 2);
the shift is a logical shift in the above example because there is no sign bit to preserve even though the high order bit is 1. So in the above example the second printf displays a as 0x3FFFFFFF and the one is not carried over.
So there is a built in logical shift operator (>>) but no arithmetic operator when talking about unsigned ints. To do an arithmetic shift on an unsigned int I guess there would be no built in operator for it because there is no good reason for there to be one. I will use my function from the first post as I think it works fine.
On second thought, maybe I could use a union and then I can logically shift or arithmetically shift it easier. Something like this:
unsigned int unsigned;
union UNION a;
a.unsigned = 0x12345678;
a.unsigned >> 1; //logical shift
a.signed >> 1; //arithmetic shift
Thank you - you helped me clear up my mistake.