Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Are these equivalent?

Reply
Thread Tools

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?
 
Reply With Quote
 
 
 
 
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.

 
Reply With Quote
 
 
 
 
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
 
Reply With Quote
 
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,
 
Reply With Quote
 
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
 
Reply With Quote
 
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?


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.
 
Reply With Quote
 
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/
 
Reply With Quote
 
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?

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
[bug]these codes can crash firefox/mozilla rockone Firefox 2 10-07-2007 01:52 PM
have you got any of these i can have spike240 Case Modding 4 09-14-2005 03:48 AM
HELP ---starting to get these alerts SHRED Firefox 1 10-09-2004 06:17 AM
[bug]these codes can crash firefox/mozilla rockone Firefox 0 09-22-2004 01:45 AM
Extensions for these? Galaxy Firefox 10 07-12-2004 11:50 PM



Advertisments