Re: bitwise AND?

From: Lamar Owen <lamar(dot)owen(at)wgcr(dot)org>
To: Vince Vielhaber <vev(at)michvhf(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: bitwise AND?
Date: 2000-08-31 18:47:50
Message-ID: 39AEA856.F5716533@wgcr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Vince Vielhaber wrote:
> Do we have any kind of bitwise AND?

Non directly.

> select foo from bar where (foo AND 2);

Ok, you _can_ do this; it's just a big pain to do so.

First, decompose any bitwise AND into TESTBIT and logical AND
operators. TESTBIT is the same as AND, just having a single bit set.

In your example, AND 2 is already a single bit. But, I'm going to
illustrate the general solution: suppose we have AND 7 -- decompose into
(foo TESTBIT 4) AND (foo TESTBIT 2) AND (foo TESTBIT 1).

Now, rewrite that to:
((foo % 8)/4)*((foo % 4)/2)*((foo % 2)/1) -- if the result is greater
than zero, the logical AND is true.

For bitwise OR, substitute integer + for integer * above.

For your case, write the query:

select foo from bar where ((foo % 4)/2)>0

AFAIK and have tested, that should work the way you think it should. (I
knew those exercise in Z80 machine language would come in handy! :-))
--
Lamar Owen
WGCR Internet Radio
1 Peter 4:11

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Rini Dutta 2000-08-31 19:22:58 optimal performance for inserts
Previous Message Florent Guillaume 2000-08-31 17:50:45 UNION/INTERSECT in subselects