Bit masks and nature of equality

Discussion in 'C Programming' started by moogyd, Jun 2, 2010.

  1. moogyd

    moogyd Guest

    Hi,

    I have debugged and fixed some code, but don't really understand the
    behavior I have seen.

    The following code compiles and runs with noo warnings (gcc 4.3.2 with
    -Wall and -pendantic)

    #include <stdio.h>
    int main() {
    int mask = 0x01 ;
    int x = 0x80 ;
    if (!(mask & x) == mask) {
    printf("Match\n") ;
    }
    return 0 ;
    }
    When I run, it prints "Match" which I don't understand
    My logic (note I am not a C programmer)
    (mask & x) Evaluates to 0
    !(mask & x) Evaluates to not 0. i.e. A positive number (I assumed
    0x01)
    So, the results of
    !(mask & x) == mask *should* be false (but it obviously isn't :-(

    My initial assumption was that the compiler was casting everything to
    a common type (bool) due to the parenthesis on (mask & x). However, in
    this case, I was hoping the gcc would warn me.

    So, can anyone provide any clarification, or confirm my assumptions.

    Thanks,

    Steven
     
    moogyd, Jun 2, 2010
    #1
    1. Advertising

  2. moogyd

    Eric Sosman Guest

    On 6/2/2010 11:13 AM, moogyd wrote:
    > Hi,
    >
    > I have debugged and fixed some code, but don't really understand the
    > behavior I have seen.
    >
    > The following code compiles and runs with noo warnings (gcc 4.3.2 with
    > -Wall and -pendantic)
    >
    > #include<stdio.h>
    > int main() {
    > int mask = 0x01 ;
    > int x = 0x80 ;
    > if (!(mask& x) == mask) {
    > printf("Match\n") ;
    > }
    > return 0 ;
    > }
    > When I run, it prints "Match" which I don't understand
    > My logic (note I am not a C programmer)
    > (mask& x) Evaluates to 0


    Right.

    > !(mask& x) Evaluates to not 0. i.e. A positive number (I assumed
    > 0x01)


    Right.

    > So, the results of
    > !(mask& x) == mask *should* be false (but it obviously isn't :-(


    Why do you think `0x1 == 0x1' should be false?

    Let's step through it again, starting with the expression
    as written:

    !(mask & x) == mask

    Substitute the value of mask:

    !(0x1 & x) == 0x1

    Substitute the value of x:

    !(0x1 & 0x80) == 0x1

    Evaluate the and:

    !(0) == 0x1

    Evaluate the not:

    1 == 0x1

    Evaluate the comparison:

    1

    Anything non-zero is "true" in C, so the `if' is satisfied and
    the printf() call is executed.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Jun 2, 2010
    #2
    1. Advertising

  3. moogyd

    crisgoogle Guest

    On Jun 2, 8:13 am, moogyd <> wrote:
    > Hi,
    >
    > I have debugged and fixed some code, but don't really understand the
    > behavior I have seen.
    >
    > The following code compiles and runs with noo warnings (gcc 4.3.2 with
    > -Wall and -pendantic)
    >
    > #include <stdio.h>
    > int main() {
    >     int mask = 0x01 ;
    >     int x = 0x80 ;
    >     if (!(mask & x) == mask) {
    >         printf("Match\n") ;
    >     }
    >     return 0 ;}
    >
    > When I run, it prints "Match" which I don't understand
    > My logic (note I am not a C programmer)
    > (mask & x)  Evaluates to 0
    > !(mask & x) Evaluates to not 0. i.e. A positive number (I assumed
    > 0x01)


    Never assume! (But you were right this time.)

    > So, the results of
    > !(mask & x) == mask *should* be false (but it obviously isn't :-(


    You must have some sort of mental block from looking at this bit of
    code for too long or something, because your analysis seems fairly
    logical except for one glaring error =)

    Yes, mask & x evaluates to 0.
    Yes, !0 evaluates to 1.

    How on earth did you then decide that 1 == 1 should be false??

    > My initial assumption was that the compiler was casting everything to
    > a common type (bool) due to the parenthesis on (mask & x). However, in
    > this case, I was hoping the gcc would warn me.


    No, parentheses or not, everything here will remain as ints. And the
    compiler never casts anything (unless you tell it to, with a cast),
    though of course it may convert, under circumstances other than what
    you're doing here.

    > So, can anyone provide any clarification, or confirm my assumptions.
    >
    > Thanks,
    >
    > Steven
     
    crisgoogle, Jun 2, 2010
    #3
  4. moogyd <> writes:
    <snip>
    > The following code compiles and runs with noo warnings (gcc 4.3.2 with
    > -Wall and -pendantic)


    You might also want -ansi (or -std=c90 or -std=c99) and maybe -Wextra.

    > #include <stdio.h>
    > int main() {
    > int mask = 0x01 ;
    > int x = 0x80 ;
    > if (!(mask & x) == mask) {
    > printf("Match\n") ;
    > }
    > return 0 ;
    > }
    > When I run, it prints "Match" which I don't understand
    > My logic (note I am not a C programmer)
    > (mask & x) Evaluates to 0


    Yup.

    > !(mask & x) Evaluates to not 0. i.e. A positive number (I assumed
    > 0x01)


    That's right. !0 is 1 -- every time on all systems. It is how C is
    specified. 0x01 is just another way to write 1 so you are right to have
    guessed that the result is 0x01. It is also 0x1 and 0x00001 and 001 and
    several others that are ways to write the same thing.

    > So, the results of
    > !(mask & x) == mask *should* be false (but it obviously isn't :-(


    I can't see why you think this. !(mask & x) is 1. mask is 0x01. 0x01
    and 1 are two ways to write the same value so they must compare equal.

    > My initial assumption was that the compiler was casting everything to
    > a common type (bool) due to the parenthesis on (mask & x). However, in
    > this case, I was hoping the gcc would warn me.
    >
    > So, can anyone provide any clarification, or confirm my assumptions.


    I'm not sure where the confusion come from.

    --
    Ben.
     
    Ben Bacarisse, Jun 2, 2010
    #4
  5. moogyd

    moogyd Guest

    On 2 June, 17:13, moogyd <> wrote:
    > Hi,
    >
    > I have debugged and fixed some code, but don't really understand the
    > behavior I have seen.
    >
    > The following code compiles and runs with noo warnings (gcc 4.3.2 with
    > -Wall and -pendantic)
    >
    > #include <stdio.h>
    > int main() {
    >     int mask = 0x01 ;
    >     int x = 0x80 ;
    >     if (!(mask & x) == mask) {
    >         printf("Match\n") ;
    >     }
    >     return 0 ;}
    >
    > When I run, it prints "Match" which I don't understand
    > My logic (note I am not a C programmer)
    > (mask & x)  Evaluates to 0
    > !(mask & x) Evaluates to not 0. i.e. A positive number (I assumed
    > 0x01)
    > So, the results of
    > !(mask & x) == mask *should* be false (but it obviously isn't :-(
    >
    > My initial assumption was that the compiler was casting everything to
    > a common type (bool) due to the parenthesis on (mask & x). However, in
    > this case, I was hoping the gcc would warn me.
    >
    > So, can anyone provide any clarification, or confirm my assumptions.
    >
    > Thanks,
    >
    > Steven


    Sorry for the noise. As people have correctly pointed out,
    0x01==0x01 !.

    In my (very limited defence), this is a simplification of the real
    problem I have, which I have been looking it all day.

    Therefore, the mental block caused me to produce a very poor example
    (and waste everyones time).

    Sorry again,

    Steven
     
    moogyd, Jun 2, 2010
    #5
  6. moogyd

    Seebs Guest

    On 2010-06-02, moogyd <> wrote:
    > The following code compiles and runs with noo warnings (gcc 4.3.2 with
    > -Wall and -pendantic)
    >
    > #include <stdio.h>
    > int main() {
    > int mask = 0x01 ;
    > int x = 0x80 ;
    > if (!(mask & x) == mask) {
    > printf("Match\n") ;
    > }
    > return 0 ;
    > }
    > When I run, it prints "Match" which I don't understand


    Consider:

    mask & x # 0
    (mask & x) == mask
    => 0 == 0x1 # 0
    !((mask & x) == mask) # 1

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
     
    Seebs, Jun 2, 2010
    #6
  7. moogyd

    Seebs Guest

    On 2010-06-02, moogyd <> wrote:
    > So, can anyone provide any clarification, or confirm my assumptions.


    Several people did, but ignore my attempted answer, I didn't sleep
    enough apparently. That was unusually dazed even for me.

    Yes, !0 == 1.

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
     
    Seebs, Jun 2, 2010
    #7
  8. moogyd

    Seebs Guest

    On 2010-06-02, Seebs <> wrote:
    > Consider:
    >
    > mask & x # 0
    > (mask & x) == mask
    > => 0 == 0x1 # 0
    > !((mask & x) == mask) # 1


    .... Also consider going back on the caffeine. Yes, that might help.

    I think I somehow convinced myself that mask was 80 and x was 1, and then
    ignored everything I knew about precedence and expression grouping to come
    up with a theory as to how this could have happened, even though it was
    obviously wrong.

    Remember: A full night of sleep is a GOOD THING.

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
     
    Seebs, Jun 2, 2010
    #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. Gianni Mariani
    Replies:
    0
    Views:
    332
    Gianni Mariani
    Jan 13, 2005
  2. bobrics
    Replies:
    2
    Views:
    717
    Flash Gordon
    Mar 21, 2006
  3. Dave

    shifts and masks

    Dave, Jun 3, 2006, in forum: C Programming
    Replies:
    4
    Views:
    449
    Malcolm
    Jun 4, 2006
  4. Dave

    replace bitfields with shifts and masks

    Dave, Jun 4, 2006, in forum: C Programming
    Replies:
    3
    Views:
    315
    Chris Torek
    Jun 4, 2006
  5. RazvanD

    Power of two alignment (bit masks)

    RazvanD, Apr 29, 2008, in forum: C Programming
    Replies:
    1
    Views:
    822
    christian.bau
    Apr 29, 2008
Loading...

Share This Page