Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: ~0 undefined?

Reply
Thread Tools

Re: ~0 undefined?

 
 
James Kanze
Guest
Posts: n/a
 
      10-20-2008
On Oct 20, 7:51*pm, blargg....@gishpuppy.com (blargg) wrote:
> Does ~0 yield undefined behavior?


No.

> C++03 section 5 paragraph 5 seems to suggest so:


> > If during the evaluation of an expression, the result is not
> > mathematically defined or not in the range of representable
> > values for its type, the behavior is undefined [...]


> The description of unary ~ (C++03 section 5.3.1 paragraph :


> > The operand of — shall have integral or enumeration type;
> > the result is the one's complement of its operand. Integral
> > promotions are performed. The type of the result is the type
> > of the promoted operand. [...]


> But perhaps "one's complement" means the value that type would
> have with all bits inverted, rather than the mathematical
> result of inverting all bits in the binary representation.


It's not really that clear what to expect on a machine not using
2's complement, but at the worst, it's unspecified or
implementation defined---not undefined behavior. (In general, I
would recommend avoiding ~, | and & on signed types.)

> For example, on a machine with 32-bit int, does one's
> complement of 0 (attempt to) have the value 2^31-1, which
> can't be represented in a signed int and is thus undefined, or
> does it have the value of whatever a signed int with all set
> bits would have (-1 on a two's complement machine)?


The wording is a bit sloppy, but what it doubtlessly means is
that you get a value with all bits set to one (in the specified
type). What that value is, of course, is probably
implementation dependent; it is -1 on a 2's complement machine,
but could very easily be 0 elsewhere.

> I used the ~0 case for simplicity; in practice, this issue
> might occur when ANDing with the complement of a mask, for
> example n&=~0x0F to clear the low 4 bits of n, or ~n&0x0F to
> find the inverted low 4 bits of n.


As long as the sign bit is 0, the behavior should be well
defined, with no ambiguities.

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      10-21-2008
On Oct 20, 9:42 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> James Kanze wrote:
> > [..]
> > The wording is a bit sloppy, but what it doubtlessly means
> > is that you get a value with all bits set to one (in the
> > specified type). What that value is, of course, is probably
> > implementation dependent; it is -1 on a 2's complement
> > machine, but could very easily be 0 elsewhere.


> Where? C++ only supports three representations, the 1's
> complement, the 2's complement, and the signed magnitude.


It's not at all clear what C++ supports. C++ took the defective
wording of C90, and modified it slightly to make it even worse.
C99 straightened it out, and does only allow three
representations. And as to where the result of ~0 would not be
-1, exactly what I said: "elsewhere [than on a 2's complement
architecture]". On 1's complement, it would be a negative 0.
(The C99 standard explicitly says that it may result in a
negative 0.) Depending on the implementation, a negative 0
either behaves exactly like a postive 0 in arithmetic operations
(but not bitwise operations), or it is undefined behavior.

So the answer to blargg's original question is, somewhat
surprisingly, that ~0 may result in undefined behavior. (Except
that since it is a constant expression, it doesn't cause
undefined behavior, but makes the program ill formed.)

More generally, the results of any of the bitwise operators --
~, |, &, ^, >> or <<, or their <op>= forms -- may result in
undefined behavior. At least according to the C99 standard; the
C++ standard doesn't really say anything meaningful about what
they do.

I currently have a paper before the committee concerning defects
in the specification of the representation of integral types,
with the proposed correction to adopt the wording from C99
(can't see any reason for C and C++ to differ here); I'll update
it to consider these issues as well. (For example, in C99, ~ is
defined as the "bitwise complement", not the 1's complement.)

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments