Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #85968 > unrolled thread

flags & flagMask == flagMask can be true for flags != flagMask if flags is a superset of flagMask ?

Started by"R.Wieser" <address@not.available>
First post2022-08-18 07:55 +0200
Last post2022-08-19 09:36 +0200
Articles 10 on this page of 30 — 12 participants

Back to article view | Back to comp.lang.c++


Contents

  flags & flagMask == flagMask can be true for flags != flagMask if flags is a superset of flagMask ? "R.Wieser" <address@not.available> - 2022-08-18 07:55 +0200
    Re: flags & flagMask == flagMask can be true for flags != flagMask if flags is a superset of flagMask ? Ike Naar <ike@sdf.org> - 2022-08-18 06:52 +0000
      Re: flags & flagMask == flagMask can be true for flags != flagMask if flags is a superset of flagMask ? "R.Wieser" <address@not.available> - 2022-08-18 09:58 +0200
        Re: flags & flagMask == flagMask can be true for flags != flagMask if flags is a superset of flagMask ? Muttley@dastardlyhq.com - 2022-08-18 14:50 +0000
          Re: flags & flagMask == flagMask can be true for flags != flagMask if flags is a superset of flagMask ? "R.Wieser" <address@not.available> - 2022-08-18 17:12 +0200
          Re: flags & flagMask == flagMask can be true for flags != flagMask if flags is a superset of flagMask ? scott@slp53.sl.home (Scott Lurndal) - 2022-08-18 15:29 +0000
            Re: flags & flagMask == flagMask can be true for flags != flagMask if flags is a superset of flagMask ? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-08-18 16:43 +0100
              Re: flags & flagMask == flagMask can be true for flags != flagMask if flags is a superset of flagMask ? scott@slp53.sl.home (Scott Lurndal) - 2022-08-18 16:10 +0000
              Re: flags & flagMask == flagMask can be true for flags != flagMask if flags is a superset of flagMask ? David Brown <david.brown@hesbynett.no> - 2022-08-18 21:19 +0200
                Re: flags & flagMask == flagMask can be true for flags != flagMask if flags is a superset of flagMask ? Manfred <noname@add.invalid> - 2022-08-19 02:57 +0200
                  Re: flags & flagMask == flagMask can be true for flags != flagMask if flags is a superset of flagMask ? David Brown <david.brown@hesbynett.no> - 2022-08-19 08:29 +0200
              Re: flags & flagMask == flagMask can be true for flags != flagMask if flags is a superset of flagMask ? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-08-19 08:12 -0700
            Re: flags & flagMask == flagMask can be true for flags != flagMask if flags is a superset of flagMask ? Mike Terry <news.dead.person.stones@darjeeling.plus.com> - 2022-08-18 16:47 +0100
              Re: flags & flagMask == flagMask can be true for flags != flagMask if Muttley@dastardlyhq.com - 2022-08-18 15:50 +0000
              Re: flags & flagMask == flagMask can be true for flags != flagMask if flags is a superset of flagMask ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-08-18 10:46 -0700
                Re: flags & flagMask == flagMask can be true for flags != flagMask if flags is a superset of flagMask ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-08-18 11:30 -0700
            Re: flags & flagMask == flagMask can be true for flags != flagMask if flags is a superset of flagMask ? Muttley@dastardlyhq.com - 2022-08-18 15:47 +0000
          Re: flags & flagMask == flagMask can be true for flags != flagMask if flags is a superset of flagMask ? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-08-19 08:53 -0700
            Re: flags & flagMask == flagMask can be true for flags != flagMask if flags is a superset of flagMask ? David Brown <david.brown@hesbynett.no> - 2022-08-19 18:00 +0200
            Re: flags & flagMask == flagMask can be true for flags != flagMask if flags is a superset of flagMask ? Muttley@dastardlyhq.com - 2022-08-19 16:10 +0000
              Re: flags & flagMask == flagMask can be true for flags != flagMask if flags is a superset of flagMask ? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-08-19 16:45 -0700
      Re: flags & flagMask == flagMask can be true for flags != flagMask if flags is a superset of flagMask ? Juha Nieminen <nospam@thanks.invalid> - 2022-08-19 06:01 +0000
        Re: flags & flagMask == flagMask can be true for flags != flagMask if flags is a superset of flagMask ? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-08-19 16:07 +0100
        Re: flags & flagMask == flagMask can be true for flags != flagMask if flags is a superset of flagMask ? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-08-19 16:08 +0100
    Re: flags & flagMask == flagMask can be true for flags != flagMask if flags is a superset of flagMask ? Mike Terry <news.dead.person.stones@darjeeling.plus.com> - 2022-08-18 16:14 +0100
      Re: flags & flagMask == flagMask can be true for flags != flagMask if flags is a superset of flagMask ? "R.Wieser" <address@not.available> - 2022-08-18 18:10 +0200
    Re: flags & flagMask == flagMask can be true for flags != flagMask if flags is a superset of flagMask ? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-08-18 11:56 -0700
      Re: flags & flagMask == flagMask can be true for flags != flagMask if flags is a superset of flagMask ? "R.Wieser" <address@not.available> - 2022-08-18 21:31 +0200
        Re: flags & flagMask == flagMask can be true for flags != flagMask if flags is a superset of flagMask ? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-08-18 21:24 -0700
          Re: flags & flagMask == flagMask can be true for flags != flagMask if flags is a superset of flagMask ? "R.Wieser" <address@not.available> - 2022-08-19 09:36 +0200

Page 2 of 2 — ← Prev page 1 [2]


#86007

FromTim Rentsch <tr.17687@z991.linuxsc.com>
Date2022-08-19 16:45 -0700
Message-ID<86h727u79r.fsf@linuxsc.com>
In reply to#86006
Muttley@dastardlyhq.com writes:

> On Fri, 19 Aug 2022 08:53:07 -0700
> Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:
>
>> Muttley@dastardlyhq.com writes:
>>
>>> If in doubt just use brackets.  They're free.
>>
>> If in doubt, look up the rule and remove the doubt.
>
> Yes, so much simpler.
>
> Very few people remember the precendence of all the operators so only an
> arrogant fool wouldn't use brackets if there was any doubt as to what it was.

Exactly.  That's why I gave the suggestion I did;  I would
much rather not be in doubt than be an arrogant fool.

[toc] | [prev] | [next] | [standalone]


#85993

FromJuha Nieminen <nospam@thanks.invalid>
Date2022-08-19 06:01 +0000
Message-ID<tdn8vo$1k5j$1@gioia.aioe.org>
In reply to#85969
Ike Naar <ike@sdf.org> wrote:
> First, == binds stronger than & in C and C++, so
> 
>   flags & flagMask == flagMask
> 
> is interpreted as
> 
>   flags & (flagMask == flagMask)

Which, in fact, is one of the most incomprehensible and annoying thing
that they did when they were inventing C (and which was then "inherited"
by C++).

Conceptually the bitwise binary operators are not that much different
from the arithmetic binary operators. Conceptually expressions like
"a+b", "a*b", "a/b", "a&b", "a^b" are doing the same thing: They are
taking two values and performing an operation on them that results
in a new value. Thus, in the same way as it's logical to write things
like:

  if(a + b == c + d)

it would seem to likewise logical to write things like:

  if(a & b == c & d)

There may be precedence differences between the binary operators, but
one would intuitively expect them all to be higher than comparison.

(Yes, *technically* speaking == is also "a binary operator that takes
two values and produces a third value from them", but not really at
the conceptual intuitive level. At the conceptual level is not really
an arithmetic operator, but is merely doing a comparison, which is not
the same thing. Thus it's logical to think that it's not the same kind
of operator as those other ones.)

[toc] | [prev] | [next] | [standalone]


#86000

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2022-08-19 16:07 +0100
Message-ID<87wnb41dbq.fsf@bsb.me.uk>
In reply to#85993
Juha Nieminen <nospam@thanks.invalid> writes:

> Ike Naar <ike@sdf.org> wrote:
>> First, == binds stronger than & in C and C++, so
>> 
>>   flags & flagMask == flagMask
>> 
>> is interpreted as
>> 
>>   flags & (flagMask == flagMask)
>
> Which, in fact, is one of the most incomprehensible and annoying thing
> that they did when they were inventing C (and which was then "inherited"
> by C++).

It came from B where it was not incomprehensible.  And in fact, early C
didn't have && and || so, just as in B, & and | were used for logical
connectives even though they were bit operations as well.

Had DMR known what C would become, I am sure he'd have sacrificed the
small benefit of not annoying the B community in order to get it right
early on.

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#86001

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2022-08-19 16:08 +0100
Message-ID<87r11c1daz.fsf@bsb.me.uk>
In reply to#85993
Juha Nieminen <nospam@thanks.invalid> writes:

> Ike Naar <ike@sdf.org> wrote:
>> First, == binds stronger than & in C and C++, so
>> 
>>   flags & flagMask == flagMask
>> 
>> is interpreted as
>> 
>>   flags & (flagMask == flagMask)
>
> Which, in fact, is one of the most incomprehensible and annoying thing
> that they did when they were inventing C (and which was then "inherited"
> by C++).

It came from B where it was not incomprehensible.  And in fact, early C
didn't have && and || so, just as in B, & and | were used for logical
connectives even though they were bit operations as well.

Had DMR known what C would become, I am sure he'd have sacrificed the
small benefit of not annoying the B community in order to get it right
early on.

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#85974

FromMike Terry <news.dead.person.stones@darjeeling.plus.com>
Date2022-08-18 16:14 +0100
Message-ID<oBGdnatm0_u-ymP_nZ2dnZfqnPXNnZ2d@brightview.co.uk>
In reply to#85968
On 18/08/2022 06:55, R.Wieser wrote:
> Hello all,
> 
> I just read a post on the "thedailywtf" website where someone said (and
> someone else confirmed) :
> 
> "flags & flagMask == flagMask can be true for flags != flagMask if flags is
> a superset of flagMask"
> 

The poster intended surely intended

    (flags & flagmast) == flagmast;

which is how you interpreted it.  This tests that every set bit in flagmast is also set in flags.

> If I translate that to assembly language (what I normally work with) it
> doesn't seem to be true :
> 
> mov eax,flags
> and eax,flagmask
> cmp eax,flagmask
> 
> AFAIK the last eax *cannot* have any bits set which are not present in
> flagmask.  

Correct, but there could be /missing/ bits.

> Therefore, can somebody explain that piece of (I take it) C+ for
> me (Whats the problem with it) ?

The test is checking that flags has no missing bits (as determined by the bits set in flagMask).

Regards,
Mike.

[toc] | [prev] | [next] | [standalone]


#85980

From"R.Wieser" <address@not.available>
Date2022-08-18 18:10 +0200
Message-ID<tdlo90$pon$1@gioia.aioe.org>
In reply to#85974
Mike,

> The poster intended surely intended
>
>    (flags & flagmast) == flagmast;
>
> which is how you interpreted it.

Yup.  But alas, its not what he wrote.   Hence my confusion toward the 
comments (not pointing out the flaw) to that code.

Regards,
Rudy Wieser

[toc] | [prev] | [next] | [standalone]


#85984

FromAndrey Tarasevich <andreytarasevich@hotmail.com>
Date2022-08-18 11:56 -0700
Message-ID<tdm20s$14u2e$1@dont-email.me>
In reply to#85968
On 8/17/2022 10:55 PM, R.Wieser wrote:
> 
> "flags & flagMask == flagMask can be true for flags != flagMask if flags is
> a superset of flagMask"
> 
> If I translate that to assembly language (what I normally work with) it
> doesn't seem to be true :
> 
> mov eax,flags
> and eax,flagmask
> cmp eax,flagmask
> 
> AFAIK the last eax *cannot* have any bits set which are not present in
> flagmask.  Therefore, can somebody explain that piece of (I take it) C+ for
> me (Whats the problem with it) ?
> 

Um... I don't see any contradiction between the original statement

   "(flags & flagMask) == flagMask can be true for flags != flagMask if 
flags is a superset of flagMask"

and your

   "AFAIK the last eax *cannot* have any bits set which are not present 
in flagmask"

So, what do you need explained and why?

You correctly stated that "eax cannot have any bits set which are not 
present in flagmask". Which is exactly why your `cmp` will see an 
equality (i.e. `==` produce a `true` result) if `flags` is a superset of 
`flagMask`.

Everything checks out. Why the question?

-- 
Best regards,
Andrey

[toc] | [prev] | [next] | [standalone]


#85986

From"R.Wieser" <address@not.available>
Date2022-08-18 21:31 +0200
Message-ID<tdm429$4og$1@gioia.aioe.org>
In reply to#85984
Andrey,

> Um... I don't see any contradiction between the original statement
>
>   "(flags & flagMask) == flagMask can be true for flags != flagMask if
> flags is a superset of flagMask"

I'm afraid you've introduced (round) brackets where my posted line doesn't
have any.   IOW, what you've posted is *not* the origional statement.

Mike terry replied with what the problem with the origional statement is :
the "==" is regarded before the "&", not after (a precendence problem).

Regards,
Rudy Wieser


[toc] | [prev] | [next] | [standalone]


#85992

FromAndrey Tarasevich <andreytarasevich@hotmail.com>
Date2022-08-18 21:24 -0700
Message-ID<tdn39b$1bdl0$1@dont-email.me>
In reply to#85986
On 8/18/2022 12:31 PM, R.Wieser wrote:
> Andrey,
> 
>> Um... I don't see any contradiction between the original statement
>>
>>    "(flags & flagMask) == flagMask can be true for flags != flagMask if
>> flags is a superset of flagMask"
> 
> I'm afraid you've introduced (round) brackets where my posted line doesn't
> have any.   IOW, what you've posted is *not* the origional statement.

Yes, but isn't that what was meant by the author of the original statement?

And you provided your own assembly translation of that original 
expression, which was obviously written for

   (flags & flagMask) == flagMask

I.e. it implies the brackets. I added the brackets because this is what 
you (or they) meant but forgot to do.

> Mike terry replied with what the problem with the origional statement is :
> the "==" is regarded before the "&", not after (a precendence problem).

That is true, but that effectively makes the whole thing nonsensical.

-- 
Best regards,
Andrey

[toc] | [prev] | [next] | [standalone]


#85995

From"R.Wieser" <address@not.available>
Date2022-08-19 09:36 +0200
Message-ID<tdnek0$1hjh$1@gioia.aioe.org>
In reply to#85992
Andrey,

>> I'm afraid you've introduced (round) brackets where my posted line 
>> doesn't
>> have any.   IOW, what you've posted is *not* the origional statement.
>
> Yes, but isn't that what was meant by the author of the original 
> statement?

Currently I would assume so, as what he wrote doesn't line up with what the 
rest of his code is doing.

> That is true, but that effectively makes the whole thing nonsensical.

Indeed.   Alas, I was not aware of the preceedence, so I didn't pick up on 
it.

Regards,
Rudy Wieser

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

Back to top | Article view | comp.lang.c++


csiph-web