Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Bit masks and nature of equality

Reply
Thread Tools

Bit masks and nature of equality

 
 
moogyd
Guest
Posts: n/a
 
      06-02-2010
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




 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      06-02-2010
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
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
 
 
 
crisgoogle
Guest
Posts: n/a
 
      06-02-2010
On Jun 2, 8:13*am, moogyd <(E-Mail Removed)> 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


 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      06-02-2010
moogyd <(E-Mail Removed)> 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.
 
Reply With Quote
 
moogyd
Guest
Posts: n/a
 
      06-02-2010
On 2 June, 17:13, moogyd <(E-Mail Removed)> 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

 
Reply With Quote
 
Seebs
Guest
Posts: n/a
 
      06-02-2010
On 2010-06-02, moogyd <(E-Mail Removed)> 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 / (E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
 
Reply With Quote
 
Seebs
Guest
Posts: n/a
 
      06-02-2010
On 2010-06-02, moogyd <(E-Mail Removed)> 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 / (E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
 
Reply With Quote
 
Seebs
Guest
Posts: n/a
 
      06-02-2010
On 2010-06-02, Seebs <(E-Mail Removed)> 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 / (E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
 
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
Power of two alignment (bit masks) RazvanD C Programming 1 04-29-2008 06:25 PM
Problem with endian notation and bit masks. Need suggestion. bobrics C Programming 2 03-21-2006 09:46 AM
A technique for compile-time detection of erroneous bit-masks -opinionsrequested Gianni Mariani C++ 0 01-13-2005 07:24 AM
64 bit - Windows Liberty 64bit, Windows Limited Edition 64 Bit, Microsoft SQL Server 2000 Developer Edition 64 Bit, IBM DB2 64 bit - new ! vvcd Computer Support 0 09-17-2004 08:15 PM
64 bit - Windows Liberty 64bit, Windows Limited Edition 64 Bit,Microsoft SQL Server 2000 Developer Edition 64 Bit, IBM DB2 64 bit - new! Ionizer Computer Support 1 01-01-2004 07:27 PM



Advertisments