9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: bakul@bitblocks.com (Bakul Shah)
Subject: [9fans] 8c puzzling behavior
Date: Tue,  8 Mar 2011 15:32:30 -0800	[thread overview]
Message-ID: <20110308233230.BD9BCB835@mail.bitblocks.com> (raw)
In-Reply-To: Your message of "Tue, 08 Mar 2011 12:16:29 EST." <a32de54ece05e5c2bfee96835e370414@ladd.quanstro.net>

On Tue, 08 Mar 2011 12:16:29 EST erik quanstrom <quanstro at quanstro.net>  wrote:
> for this
> 
> 	uvlong u, i;
> 
> 	i = 31;
> 	u = 0xff00000000000000ull;
> 
> i get
> 
> 	u & 1<<i		-> 0xff00000000000000ull
> 	u & 0x80000000		-> 0
> 	u & (int)0x80000000	-> 0xff00000000000000ull
> 	u & -2147483648		-> 0
> 
> to put a finer point on it,
> 
> 	(uvlong)(1<<i)		-> 0xffffffff80000000
> 	(uvlong)(0x80000000)	-> 0x80000000
> 	(uvlong)(int)0x80000000	-> 0xffffffff80000000
> 	(uvlong)-2147483648	-> 0x80000000
> 
> so it seems clear that constants are treated as if unsigned, regardless,
> but variables are not?
> 
> can this be correct?  i hate to hazzard guesses about the c standard.

1 is signed. 1u is unsigned (see 6.4.4.1). 0x80000000 doesn't
fit in an int so it is an unsigned int. 0x100000000 won't fit
in an unsigned int so it will pick the next type in the table
that is big enough, which is long long int. Nice, eh?!

For shift operators, type of the result is that of the
"promoted left operand" (see 6.5.7). So 1<<i is an int.
It is first promoted to a long long int and then to
unsigned long long int.

As for conversions, a smaller sized (lower ranked) value is
first converted to a larger sized (higher ranked) value of the
*same* type before any signedness conversion but can't find
where it says so in the std. May be 6.3.1.1.

The value of (int)0x80000000 is an unsigned int to int
conversion and since its value doesn't fit in an int, the
result is implementation defined. Usually it just becomes
-2147483648 (since that is more efficient than anything else).

Both clang and gcc fail the following (but I can well believe
plan9 C compiler behaves differently).

> 	u & -2147483648		-> 0
> 	(uvlong)-2147483648	-> 0x80000000

-2147483648 is first converted to long long int (preserving
its value) and then to unsigned long long int. So this is
0xffffffff80000000ull.

---

#define check(x) do if (!(x)) printf("%s:%d: check failed: %s\n", __FILE__, __LINE__, #x); while(0)

typedef unsigned long long uvlong;

int main()
{
	uvlong i = 31;
	uvlong u = 0xff00000000000000ull;

	check((u & 1<<i)              == 0xff00000000000000ull);
	check((u & 0x80000000)        == 0);
	check((u & (int)0x80000000)   == 0xff00000000000000ull);
	check((u & -2147483648)       == 0);

	check((uvlong)(1<<i)          == 0xffffffff80000000ull);
	check((uvlong)(0x80000000)    == 0x80000000ull);
	check((uvlong)(int)0x80000000 == 0xffffffff80000000ull);
	check((uvlong)-2147483648     == 0x80000000ull);
	check((uvlong)-2147483648     == 0xffffffff80000000ull);
	return 0;
}



  parent reply	other threads:[~2011-03-08 23:32 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-03-08 17:16 erik quanstrom
2011-03-08 20:21 ` geoff at plan9.bell-labs.com
2011-03-08 23:32 ` Bakul Shah [this message]
2011-03-08 23:54   ` erik quanstrom
2011-03-09  7:23     ` Charles Forsyth
2011-03-09 13:05       ` erik quanstrom
2011-03-08 17:21 erik quanstrom
2011-03-08 17:43 ` Lucio De Re

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=20110308233230.BD9BCB835@mail.bitblocks.com \
    --to=bakul@bitblocks.com \
    /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).