Are these equivalent?

D

Donald Canton

Are the following two statements equivalent?

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

Yes or no?
 
T

Tom St Denis

Donald said:
Are the following two statements equivalent?

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

Yes or no?

No.
 
R

Richard Heathfield

Donald said:
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?
 
B

bd

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,
 
M

Micah Cowan

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
 
D

Donald Canton

Are the following two statements equivalent?

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

Yes or no?

Thanks for the answers so far. Here is more information that I
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.
 
E

Emmanuel Delahaye

In said:
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...
 
R

Richard Heathfield

Donald said:
(e-mail address removed) (Donald Canton) wrote in message


Thanks for the answers so far. Here is more information that I
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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top