Shifting the result of a cast does not produce the same results as when using
an intermediate variable.
short x = -1;
unsigned a = ((unsigned)(short)-1) >> 1;
unsigned tmp = (unsigned)x;
unsigned b = tmp >> 1;
unsigned c = ((unsigned)x) >> 1;
printf("a:%x b:%x c:%x",a,b,c);
produces
a:7fffffff b:7fffffff c:ffffffff
while I'd expect the result for c to be the same as the other two.
Similar behavior appears to occur with other types, as long as the source type
is shorter than the target type and the target type is int or longer.
--
Summary: short variable cast to unsigned int fails to right shift
as unsigned
Product: gcc
Version: 4.1.2
Status: UNCONFIRMED
Severity: major
Priority: P3
Component: c
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: fgccbz1 at greynode dot net
GCC host triplet: x86_64-redhat-linux
GCC target triplet: x86_64-redhat-linux
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34235