Velocity Reviews > Are these equivalent?

# Are these equivalent?

Donald Canton
Guest
Posts: n/a

 07-04-2003
Are the following two statements equivalent?

bitField = (bitField & (--bitField));
bitField = (bitField & (bitField-1));

Yes or no?

Tom St Denis
Guest
Posts: n/a

 07-04-2003
Donald Canton wrote:
> Are the following two statements equivalent?
>
> bitField = (bitField & (--bitField));
> bitField = (bitField & (bitField-1));
>
> Yes or no?

No.

Richard Heathfield
Guest
Posts: n/a

 07-04-2003
Donald Canton wrote:

> Are the following two statements equivalent?
>
> bitField = (bitField & (--bitField));
> bitField = (bitField & (bitField-1));
>
> Yes or no?

No. The first invokes undefined behaviour. The behaviour and legality of the
second depends on various things. For example:

what type have you given to bitField?
If it really is a bit-field, is it signed, or unsigned?
How many bits have you assigned to it?
What are the possible values it might have before the above code is
executed?

--
Richard Heathfield : http://www.velocityreviews.com/forums/(E-Mail Removed)
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton

bd
Guest
Posts: n/a

 07-04-2003
On Fri, 04 Jul 2003 14:07:53 -0700, Donald Canton wrote:

> Are the following two statements equivalent?
>
> bitField = (bitField & (--bitField));

This is Undefined Behavior. You can't modify a variable twice without an
intervening sequence point, and you can't access and modify without an
intervening sequence point either. That statement could do anything,

Micah Cowan
Guest
Posts: n/a

 07-05-2003
(E-Mail Removed) (Donald Canton) writes:

> Are the following two statements equivalent?
>
> bitField = (bitField & (--bitField));
> bitField = (bitField & (bitField-1));

The first statement yields undefined behavior, as bitField is modified
twice before a sequence point (there is only one, and it occurs
after the entire statement has been executed). Even if it had behavior
which was perfectly defined to the most "obvious" behavior, the --
operator would just require extra work, wouldn't it? ...Assigning to
bitField and then discarding that new value a moment later?

-Micah

Donald Canton
Guest
Posts: n/a

 07-05-2003
(E-Mail Removed) (Donald Canton) wrote in message news:<(E-Mail Removed). com>...
> Are the following two statements equivalent?
>
> bitField = (bitField & (--bitField));
> bitField = (bitField & (bitField-1));
>
> Yes or no?

should've included with the original post:

The variable bitField is of type int and on my implementation an int
is 32 bits, two's complement.

The first statement was translated from Java, where it apparently
worked exactly as the second. (I think I remember reading that Java
guarantees left-to-right execution? Don't know.)

Anyway, I looked in my K&R for sequence points and couldn't find
anything. Can someone please elaborate on sequence points? Thanks.

Emmanuel Delahaye
Guest
Posts: n/a

 07-05-2003
In 'comp.lang.c', (E-Mail Removed) (Donald Canton) wrote:

> Are the following two statements equivalent?
>
> bitField = (bitField & (--bitField));
> bitField = (bitField & (bitField-1));
>
> Yes or no?

No. The first one modifies 'bitField'. I won't bet on the result...

--
-ed- (E-Mail Removed) [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/

Richard Heathfield
Guest
Posts: n/a

 07-05-2003
Donald Canton wrote:

> (E-Mail Removed) (Donald Canton) wrote in message
> news:<(E-Mail Removed). com>...
>> Are the following two statements equivalent?
>>
>> bitField = (bitField & (--bitField));
>> bitField = (bitField & (bitField-1));
>>
>> Yes or no?

>
> should've included with the original post:
>
> The variable bitField is of type int and on my implementation an int
> is 32 bits, two's complement.

So it's nothing to do with bitfields, then. Okay, so let's make that clear
by dropping the word "bitField" from the second (the legal) expression to
make it:

x = x & (x - 1);

This has well-defined behaviour provided x's initial value is not INT_MIN.

<snip>

--
Richard Heathfield : (E-Mail Removed)
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton