Path: csiph.com!news.mixmin.net!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Ike Naar Newsgroups: comp.lang.c++ Subject: Re: flags & flagMask == flagMask can be true for flags != flagMask if flags is a superset of flagMask ? Date: Thu, 18 Aug 2022 06:52:31 -0000 (UTC) Organization: A noiseless patient Spider Lines: 43 Message-ID: References: Injection-Date: Thu, 18 Aug 2022 06:52:31 -0000 (UTC) Injection-Info: reader01.eternal-september.org; posting-host="7294b30a4edeb13dcac91ee7805fb756"; logging-data="948480"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+mJBy3eH9hBpFzzMipatSz" User-Agent: slrn/1.0.3 (Patched for libcanlock3) (NetBSD) Cancel-Lock: sha1:xLaxZM1Sh4aEglXwDZZyUBx3r1A= Xref: csiph.com comp.lang.c++:85969 On 2022-08-18, 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" > > 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) ? First, == binds stronger than & in C and C++, so flags & flagMask == flagMask is interpreted as flags & (flagMask == flagMask) which can be simplified to flags & 1 which is true if the least significant bit of flags is set. So, for example, if flags = 1 and flagMask = 0 then flags & (flagMask == flagMask) is true for flags != flagMask. Your assembly code suggests that you have the priorities of & and == reversed, but even in that case, for flags = 1 and flagMask = 0, --> flags is a superset of flagMask --> flags != flagMask --> ((flags & flagMask) == flagMask) equals ((1 & 0) == 0) equals (0 == 0) equals true