Are these equivalent?

Discussion in 'C Programming' started by Donald Canton, Jul 4, 2003.

  1. Are the following two statements equivalent?

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

    Yes or no?
     
    Donald Canton, Jul 4, 2003
    #1
    1. Advertising

  2. Donald Canton

    Tom St Denis Guest

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


    No.
     
    Tom St Denis, Jul 4, 2003
    #2
    1. Advertising

  3. 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 :
    "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
     
    Richard Heathfield, Jul 4, 2003
    #3
  4. Donald Canton

    bd Guest

    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,
     
    bd, Jul 4, 2003
    #4
  5. Donald Canton

    Micah Cowan Guest

    (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
     
    Micah Cowan, Jul 5, 2003
    #5
  6. (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.
     
    Donald Canton, Jul 5, 2003
    #6
  7. In 'comp.lang.c', (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- [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/
     
    Emmanuel Delahaye, Jul 5, 2003
    #7
  8. Donald Canton wrote:

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

    <snip>

    --
    Richard Heathfield :
    "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
     
    Richard Heathfield, Jul 5, 2003
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    449
  2. HDL Book Seller
    Replies:
    0
    Views:
    460
    HDL Book Seller
    Aug 6, 2004
  3. Page
    Replies:
    1
    Views:
    491
    Michael Borgwardt
    Sep 9, 2004
  4. Replies:
    2
    Views:
    345
  5. Coolgg
    Replies:
    9
    Views:
    186
    Coolgg
    Jan 24, 2013
Loading...

Share This Page