Thread: C question about bitmasks in datetime.c

C question about bitmasks in datetime.c

From
Bruce Momjian
Date:
I see a few cases of this code in src/backend/utils/adt/datetime.c:
   else if ((fmask & DTK_DATE_M) != DTK_DATE_M)

Wouldn't this be clearer as:
   else if (fmask & DTK_DATE_M)

--  Bruce Momjian  <bruce@momjian.us>        http://momjian.us EnterpriseDB
http://enterprisedb.com
 + It's impossible for everything to be true. +



Re: C question about bitmasks in datetime.c

From
Andres Freund
Date:
On 2013-10-01 11:15:36 -0400, Bruce Momjian wrote:
> I see a few cases of this code in src/backend/utils/adt/datetime.c:
> 
>     else if ((fmask & DTK_DATE_M) != DTK_DATE_M)
> 
> Wouldn't this be clearer as:
> 
>     else if (fmask & DTK_DATE_M)

That doesn't have the same meaning. The latter is trueif only one bit of
DTK_DATE_M is set, while the former requires all bits to be set.

Greetings,

Andres Freund

-- Andres Freund                       http://www.2ndQuadrant.com/PostgreSQL Development, 24x7 Support, Training &
Services



Re: C question about bitmasks in datetime.c

From
Bruce Momjian
Date:
On Tue, Oct  1, 2013 at 05:17:35PM +0200, Andres Freund wrote:
> On 2013-10-01 11:15:36 -0400, Bruce Momjian wrote:
> > I see a few cases of this code in src/backend/utils/adt/datetime.c:
> > 
> >     else if ((fmask & DTK_DATE_M) != DTK_DATE_M)
> > 
> > Wouldn't this be clearer as:
> > 
> >     else if (fmask & DTK_DATE_M)
> 
> That doesn't have the same meaning. The latter is trueif only one bit of
> DTK_DATE_M is set, while the former requires all bits to be set.

Oh, I see it now --- DTK_DATE_M is not a bit but rather a set of bits,
and they are testing if _all_ are set.  Thank you!

--  Bruce Momjian  <bruce@momjian.us>        http://momjian.us EnterpriseDB
http://enterprisedb.com
 + It's impossible for everything to be true. +