Re: compare a large number of variables



"Tim Rentsch" <txr@xxxxxxxxxxxxxxxxxxx> wrote in message
news:kfnwtm38cgh.fsf@xxxxxxxxxxxxxxxxxxxxxx
> "Mark B" <sober@xxxxxxxxxxxx> writes:
>
> I'm going to try to trim this down to just the relevant
> portions...

Although we disagree on what posts are pertinent to a
particular branch, I'll follow your lead and trim even more.

> unsigned char a = 3, b = 5, c = 8;
> if(a-b|b-c) ...
>
> The arguments of 'a-b' and 'b-c' are promoted (assuming
> UCHAR_MAX < INT_MAX) to 'int', and the results therefore
> are negative, with no overflow.

OK.

> Doing a bitwise OR of
> negative operands means undefined behavior; 6.5 p4.

If so, I'm missing something and would appreciate a point in the
right direction. As you know, I'm using the draft... is this what
you are in fact referring to? (if not please post appropriate text)
6.5 Expressions
...
[#4] Some operators (the unary operator ~, and the binary
operators <<, >>, &, ^, and |, collectively described as
bitwise operators) are required to have operands that have
integer type. These operators return values that depend on
the internal representations of integers, and have
implementation-defined and undefined aspects for signed
types.

The 'undefined aspects' are later identified (when applicable) in the
appropriate sections which define the operators individually.
For example, the section describing bitwise shift operators explicitly
states the specific circumstances in which using signed integer types
results in either undefined behavior, or implementation defined behavior.
(both are possible)

I don't see anything in the section which describes the inclusive OR
(6.5.12 in the draft) which cautions against using a negative operand.
The only requirement seems to be that each of the operands have
an integer type. What am I missing (or misinterpreting?)

Respectfully,
Mark


.



Relevant Pages

  • Re: compare a large number of variables
    ... a case of undefined behavior is called ... >> may generate a bit pattern that corresponds to a "negative ...
    (comp.lang.c)
  • Re: c = foo(--a) + a; ?
    ... Kenneth Brody wrote: ... It means that something might or might not be undefined behavior. ... the right operand has an integer type. ... Assuming that there's a '*' missing at the beggining of that statement, then you're converting an arbitrary integer to a pointer type ...
    (comp.lang.c)
  • Re: unsigned short addition/subtraction overflow
    ... > James Hu wrote: ... > and undefined behavior in that case. ... corresponding unsigned integer type, and the representation of the same ...
    (comp.lang.c)