This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

signed/unsigned right shift


Hello,

I have the following program which takes 2 unsigned short values,
multiplies them and shifts the result right.

This right shift gives a value with 1s bits at the high end. I think
it should give 0s there.


---- test program ---
unsigned int bug(long long uu, long long vv)
{
  union {
    unsigned long long ll;
    unsigned short s[4];
  } u, v;

  u.ll = (unsigned long long)uu;
  v.ll = (unsigned long long)vv;

  printf("u.s[0] = %04x, v.s[1] = %04x\n", u.s[0], v.s[1]);

  return (u.s[0] * v.s[1]) >> 16;
}

int main(void)
{
  long long u;
  long long v;

  u = 0x8f669000ll;
  v = 0xe4f06000ll;

  printf("%08x\n", bug(u, v));
  return 0;
}
---- test program ---

The output is 


u.s[0] = 9000, v.s[1] = e4f0
ffff80c7


If I write the last line of the bug function like

return (unsigned)(u.s[0] * v.s[1]) >> 16;

it works like I expected and gives


u.s[0] = 9000, v.s[1] = e4f0
000080c7


I post this here since with 3 different compilers tried only gcc gives
this result.

Sorry if this behaviour is a C feature I shouldn't have asked here,
and if it is, please point me to the according section in the
standard.

regards,
chris


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]