9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: Bakul Shah <bakul+plan9@bitblocks.com>
To: Fans of the OS Plan 9 from Bell Labs <9fans@9fans.net>
Subject: Re: [9fans] portability question
Date: Wed, 16 Jun 2010 10:52:10 -0700	[thread overview]
Message-ID: <20100616175211.106945B2E@mail.bitblocks.com> (raw)
In-Reply-To: Your message of "Wed, 16 Jun 2010 11:11:09 +0200." <AANLkTinYjxYQEDBK5u2kohjRwdZA-wIB9hK8nVVnITuV@mail.gmail.com>

On Wed, 16 Jun 2010 11:11:09 +0200 hugo rivera <uair00@gmail.com>  wrote:
> Can someone clarify why the program included outputs 'AB000000' (as I
> expect) on 32 bit systems and 'FFFFFFFFAB000000' on 64 bit systems?
> where all those 1's came from? what's the portable way of doing this?
> sorry for newbie questions like this.
>
>
>        unsigned long l;
>         unsigned char c;
>
>         l = 0L;
>         c = 0xAB;
>         l |= c << 24;
>         printf("%lX\n", l);

For use of C on non-plan9 machines I would recommend
downloading the last draft of the C9x standard as a ready
reference.  Google for n843.

As per Section 6.5.7 of C9x, both operands of << must be of
"integer type" and the result type is that of the left
operand. Since sizeof c < sizeof(int), it is promoted.  Now
6.3.1.2 says "if an int can represent all values of the
original type, the value is converted to an int". So c is
first converted to an int which means c << 24 is an integer
and -ve. Since an int is smaller than a long (in your case)
it is promoted to a long. Changing the |= statement to

         l |= (unsigned)c << 24;

should give you what you want.



  parent reply	other threads:[~2010-06-16 17:52 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-06-16  9:11 hugo rivera
2010-06-16 10:01 ` Lucio De Re
2010-06-16 10:15   ` hugo rivera
2010-06-16 10:27   ` Charles Forsyth
2010-06-16 13:30 ` maht
2010-06-16 15:26   ` erik quanstrom
2010-06-16 17:52 ` Bakul Shah [this message]
2010-06-17  7:50   ` hugo rivera

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=20100616175211.106945B2E@mail.bitblocks.com \
    --to=bakul+plan9@bitblocks.com \
    --cc=9fans@9fans.net \
    /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 NNTP newsgroup(s).