454,436 Members | 1,344 Online
Need help? Post your question and get tips & solutions from a community of 454,436 IT Pros & Developers. It's quick & easy.

# Are these equivalent?

 P: n/a Are the following two statements equivalent? bitField = (bitField & (--bitField)); bitField = (bitField & (bitField-1)); Yes or no? Nov 13 '05 #1
7 Replies

 P: n/a Donald Canton wrote: Are the following two statements equivalent? bitField = (bitField & (--bitField)); bitField = (bitField & (bitField-1)); Yes or no? No. Nov 13 '05 #2

 P: n/a 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 : bi****@eton.powernet.co.uk "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 Nov 13 '05 #3

 P: n/a 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, Nov 13 '05 #4

 P: n/a dc*****@rocketmail.com (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 Nov 13 '05 #5

 P: n/a dc*****@rocketmail.com (Donald Canton) wrote in message news:... 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. Nov 13 '05 #6

 P: n/a In 'comp.lang.c', dc*****@rocketmail.com (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- em**********@noos.fr [remove YOURBRA before answering me] The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/ Nov 13 '05 #7

 P: n/a Donald Canton wrote: dc*****@rocketmail.com (Donald Canton) wrote in message news:... 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. 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. -- Richard Heathfield : bi****@eton.powernet.co.uk "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 Nov 13 '05 #8

### This discussion thread is closed

Replies have been disabled for this discussion.