public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Christian Groessler <cpg@aladdin.de>
To: gcc@gcc.gnu.org
Subject: signed/unsigned right shift
Date: Fri, 12 Mar 2004 21:25:00 -0000	[thread overview]
Message-ID: <87wu5pwyg7.fsf@aladdin.de> (raw)

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

             reply	other threads:[~2004-03-12 21:25 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-03-12 21:25 Christian Groessler [this message]
2004-03-12 22:24 ` Andreas Schwab
2004-03-12 22:37   ` Dale Johannesen
2004-03-14 18:09 Christian Groessler
2004-03-14 18:23 ` Ian Lance Taylor
2004-03-15 22:02 Christian Groessler
2004-03-15 22:14 ` Andreas Schwab
2004-03-15 22:18   ` Andreas Schwab
2004-03-15 22:17 ` Ian Lance Taylor
2004-03-15 22:24 Christian Groessler

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87wu5pwyg7.fsf@aladdin.de \
    --to=cpg@aladdin.de \
    --cc=gcc@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).