Re: bitwise AND? - Mailing list pgsql-hackers

From Lamar Owen
Subject Re: bitwise AND?
Date
Msg-id 39AEA856.F5716533@wgcr.org
Whole thread Raw
In response to bitwise AND?  (Vince Vielhaber <vev@michvhf.com>)
List 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


pgsql-hackers by date:

Previous
From: Mark Hollomon
Date:
Subject: new relkind for view
Next
From: Rini Dutta
Date:
Subject: optimal performance for inserts