operators and bit field

Started by Dennis Gearonabout 23 years ago6 messagesgeneral
Jump to latest
#1Dennis Gearon
gearond@cvc.net

I haven't seen what operators can be applied to bit masks.

In particular, I am looking to use binary AND:

I want to AND two bitmasks together and see:

A/ if the result has ANY bits set
B/ what bits are set in the results.

SELECT other_fields
FROM some_table
WHERE
( bitmask_8_columnA & bitmask_8_column B) = bitmask_8_patternONE
OR ( bitmask_8_columnA & bitmask_8_column B) = bitmask_8_patternTWO
;

I may try to use a single char for one of the bit masks I need,
is it possible to store all 256 values into a varchar(1) using
escaped input?

Is it possible to AND two varchars within a select and compare the result?

SELECT other_fields
FROM some_table
WHERE ( varchar_1_columnA & varchar_1_columnB ) = 'single_escaped_char_string';

#2Bruno Wolff III
bruno@wolff.to
In reply to: Dennis Gearon (#1)
Re: operators and bit field

On Fri, Feb 14, 2003 at 11:12:15 -0800,
Dennis Gearon <gearond@cvc.net> wrote:

I haven't seen what operators can be applied to bit masks.

It looks like & works:
area=> select 3&6;
?column?
----------
2
(1 row)

Show quoted text

In particular, I am looking to use binary AND:

I want to AND two bitmasks together and see:

A/ if the result has ANY bits set
B/ what bits are set in the results.

SELECT other_fields
FROM some_table
WHERE
( bitmask_8_columnA & bitmask_8_column B) = bitmask_8_patternONE
OR ( bitmask_8_columnA & bitmask_8_column B) = bitmask_8_patternTWO
;

I may try to use a single char for one of the bit masks I need,
is it possible to store all 256 values into a varchar(1) using
escaped input?

Is it possible to AND two varchars within a select and compare the result?

SELECT other_fields
FROM some_table
WHERE ( varchar_1_columnA & varchar_1_columnB ) = 'single_escaped_char_string';

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

#3Dennis Gearon
gearond@cvc.net
In reply to: Bruno Wolff III (#2)
Re: operators and bit field

Bruno, could you check that with explicity bitfield(n) values, please?

2/14/2003 1:37:03 PM, Bruno Wolff III <bruno@wolff.to> wrote:

Show quoted text

On Fri, Feb 14, 2003 at 11:12:15 -0800,
Dennis Gearon <gearond@cvc.net> wrote:

I haven't seen what operators can be applied to bit masks.

It looks like & works:
area=> select 3&6;
?column?
----------
2
(1 row)

In particular, I am looking to use binary AND:

I want to AND two bitmasks together and see:

A/ if the result has ANY bits set
B/ what bits are set in the results.

SELECT other_fields
FROM some_table
WHERE
( bitmask_8_columnA & bitmask_8_column B) = bitmask_8_patternONE
OR ( bitmask_8_columnA & bitmask_8_column B) = bitmask_8_patternTWO
;

I may try to use a single char for one of the bit masks I need,
is it possible to store all 256 values into a varchar(1) using
escaped input?

Is it possible to AND two varchars within a select and compare the result?

SELECT other_fields
FROM some_table
WHERE ( varchar_1_columnA & varchar_1_columnB ) = 'single_escaped_char_string';

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

#4Dennis Gearon
gearond@cvc.net
In reply to: Bruno Wolff III (#2)
Re: operators and bit field

Also, for fun, could you try '3' & '6' ?

2/14/2003 1:37:03 PM, Bruno Wolff III <bruno@wolff.to> wrote:

Show quoted text

On Fri, Feb 14, 2003 at 11:12:15 -0800,
Dennis Gearon <gearond@cvc.net> wrote:

I haven't seen what operators can be applied to bit masks.

It looks like & works:
area=> select 3&6;
?column?
----------
2
(1 row)

In particular, I am looking to use binary AND:

I want to AND two bitmasks together and see:

A/ if the result has ANY bits set
B/ what bits are set in the results.

SELECT other_fields
FROM some_table
WHERE
( bitmask_8_columnA & bitmask_8_column B) = bitmask_8_patternONE
OR ( bitmask_8_columnA & bitmask_8_column B) = bitmask_8_patternTWO
;

I may try to use a single char for one of the bit masks I need,
is it possible to store all 256 values into a varchar(1) using
escaped input?

Is it possible to AND two varchars within a select and compare the result?

SELECT other_fields
FROM some_table
WHERE ( varchar_1_columnA & varchar_1_columnB ) = 'single_escaped_char_string';

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

#5Tom Lane
tgl@sss.pgh.pa.us
In reply to: Dennis Gearon (#1)
Re: operators and bit field

Dennis Gearon <gearond@cvc.net> writes:

I haven't seen what operators can be applied to bit masks.
In particular, I am looking to use binary AND:
I want to AND two bitmasks together and see:
A/ if the result has ANY bits set
B/ what bits are set in the results.

Why don't you use bit strings, like the SQL spec authors intended?

regression=# select b'10001' & b'01111';
?column?
----------
00001
(1 row)

See types BIT(n) and BIT VARYING(n).

regards, tom lane

#6Dennis Gearon
gearond@cvc.net
In reply to: Tom Lane (#5)
Re: operators and bit field

Sorry,

'bit masks' === 'bit strings'

BTW, how are 'bit strings' stored and manipulated?

2/14/2003 2:29:48 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:

Show quoted text

Dennis Gearon <gearond@cvc.net> writes:

I haven't seen what operators can be applied to bit masks.
In particular, I am looking to use binary AND:
I want to AND two bitmasks together and see:
A/ if the result has ANY bits set
B/ what bits are set in the results.

Why don't you use bit strings, like the SQL spec authors intended?

regression=# select b'10001' & b'01111';
?column?
----------
00001
(1 row)

See types BIT(n) and BIT VARYING(n).

regards, tom lane