From: Al Viro <viro@ZenIV.linux.org.uk>
To: Bernd Petrovitsch <bernd@petrovitsch.priv.at>
Cc: Julia Lawall <julia@diku.dk>, Dan Carpenter <error27@gmail.com>,
kernel-janitors@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: bug list: assigning negative values to unsigned variables
Date: Wed, 27 Jan 2010 14:44:44 +0000 [thread overview]
Message-ID: <20100127144444.GM19799@ZenIV.linux.org.uk> (raw)
In-Reply-To: <1264601569.10795.131.camel@localhost>
On Wed, Jan 27, 2010 at 03:12:49PM +0100, Bernd Petrovitsch wrote:
> On Mit, 2010-01-27 at 13:30 +0100, Julia Lawall wrote:
> > On Wed, 27 Jan 2010, Bernd Petrovitsch wrote:
> > > On Mit, 2010-01-27 at 11:57 +0100, Julia Lawall wrote:
> > > > On Wed, 27 Jan 2010, Dan Carpenter wrote:
> > > >
> > > > > Fixing the places which assign negative values to unsigned variables is a good janitor task.
> > > >
> > > > I had the impression that assignment to -1 was done sometimes as a
> > > > portable way to initialize the variable to 0xffff (for any number of f's).
> Hmm, perhaps some experienced language lawyer can comment on the
> "portable".
Doesn't take a lawyer; conversion to unsigned types *IS* portable and defined
as arithmetics modulo 2^{width}. In particular, for any unsigned type T you
are going to have the same results from (T)-1 and ~(T)0 (and (T)-1L, etc.).
The value being converted is interpreted as an integer (i.e. the element of
$\Bbb Z$) and then taken modulo 2^{width}, regardless of the type it had
come from. So -1 is just fine and will result in 0xff....f of the right
width.
It's conversion to signed that is a mess if the value you are converting isn't
already in range representable by the type you are converting to.
Whether a specific example of conversion of negative to unsigned is a good
idea stylistically is a different question, of course, but that should be
taken on case-to-case basis.
next prev parent reply other threads:[~2010-01-27 14:44 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-01-27 10:40 bug list: assigning negative values to unsigned variables Dan Carpenter
2010-01-27 10:57 ` Julia Lawall
2010-01-27 11:09 ` Bernd Petrovitsch
2010-01-27 12:30 ` Julia Lawall
2010-01-27 14:12 ` Bernd Petrovitsch
2010-01-27 14:44 ` Al Viro [this message]
2010-01-27 11:13 ` Dan Carpenter
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=20100127144444.GM19799@ZenIV.linux.org.uk \
--to=viro@zeniv.linux.org.uk \
--cc=bernd@petrovitsch.priv.at \
--cc=error27@gmail.com \
--cc=julia@diku.dk \
--cc=kernel-janitors@vger.kernel.org \
--cc=linux-kernel@vger.kernel.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