Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

SQL bitwise operator ?

0 views
Skip to first unread message

mchd

unread,
Jan 29, 2003, 5:08:28 AM1/29/03
to
Hi all,

I want to select from a table records that have a particular bit hold in a
field of type integer. Is there a way to do this in a simple way ?

Thanks for any help


Simon Lewis

unread,
Jan 29, 2003, 6:22:21 AM1/29/03
to
Hi,

The easiest way to do this is to write a "BitwiseAnd" function:
Function BitwiseAnd(x As Long, y As Long) As Long
BitWiseAnd = x And y
End Function
then use this in your query, e.g.:
Select * from MyTable Where BitwiseAnd(MyField, 4)>0
to select records with bit 2 of MyField set.

----
Simon Lewis


"mchd" <mc...@wanadoo.fr> wrote in message
news:OLWC943xCHA.2596@TK2MSFTNGP12...

Marshall Barton

unread,
Jan 29, 2003, 10:57:28 AM1/29/03
to
mchd wrote:

>I want to select from a table records that have a particular bit hold in a
>field of type integer. Is there a way to do this in a simple way ?

I usually use a function like Simon suggested, but in some
situations that's been slow.

**If you only need to test a single bit** and it's not the
high (sign) bit, an alternative is to use an expression in
the query:

(field \ (2 ^ bitnum)) Mod 2 <> 0

--
Marsh
MVP [MS Access]

0 new messages