Subject | Re: Bit operations in Firebird SQL |
---|---|

Author | Ali Gökçen |

Post date | 2005-09-28T08:07:39Z |

Hi,

there is no internal bitwise operator support in FB but you can do it

yourself some math operations.

here is your example:

WHERE (BIT_MASK AND 3)

is same as

WHERE (BIT_MASK-(BIT_MASK/4)*4))=3

lets try it with mask 01010111 = 2^0+2^1+2^2+2^4+2^6=1+2+4+16+64=87

87/4=21, 21*4=84, 87-84 = 3

dividing with 2 is means shifting one bit to right side.

we are elimanting right 2 bits by dividing by 4 (2 times 2)

then multipling this value with 4 again, now last two bits are zero

then substraction it from orijinal value gives us the last 2 bits.

a bit assaembly.. thats all.

If you are using bit field you should familiar with bit operations

like this..

if you want clean code in where operations then you can create

computed by bit values of that field.

BIT0 computed by(case when BITMASK-(BITMASK/2)*2=1 then 1 else 0 end)

BIT1 computed by(case when BITMASK-(BITMASK/4)*4>=2 then 1 else 0 end)

BIT3 computed by(case when BITMASK-(BITMASK/8)*8>=4 then 1 else 0 end)

for your example:

where (BIT0=1 AND BIT1=1)

etc..

Regards.

Ali

--- In firebird-support@yahoogroups.com, "mobiltexmark" <mbentley-

keyword-yahoo.ae8df5@m...> wrote:

there is no internal bitwise operator support in FB but you can do it

yourself some math operations.

here is your example:

WHERE (BIT_MASK AND 3)

is same as

WHERE (BIT_MASK-(BIT_MASK/4)*4))=3

lets try it with mask 01010111 = 2^0+2^1+2^2+2^4+2^6=1+2+4+16+64=87

87/4=21, 21*4=84, 87-84 = 3

dividing with 2 is means shifting one bit to right side.

we are elimanting right 2 bits by dividing by 4 (2 times 2)

then multipling this value with 4 again, now last two bits are zero

then substraction it from orijinal value gives us the last 2 bits.

a bit assaembly.. thats all.

If you are using bit field you should familiar with bit operations

like this..

if you want clean code in where operations then you can create

computed by bit values of that field.

BIT0 computed by(case when BITMASK-(BITMASK/2)*2=1 then 1 else 0 end)

BIT1 computed by(case when BITMASK-(BITMASK/4)*4>=2 then 1 else 0 end)

BIT3 computed by(case when BITMASK-(BITMASK/8)*8>=4 then 1 else 0 end)

for your example:

where (BIT0=1 AND BIT1=1)

etc..

Regards.

Ali

--- In firebird-support@yahoogroups.com, "mobiltexmark" <mbentley-

keyword-yahoo.ae8df5@m...> wrote:

> I have a field that defines a bitmask and would like to have the sqlDelphi.

> search return data based on the mask, but can not seem to find

> anywhere describing bit operations. What I would like is something

> like:

>

> SELECT * FROM TABLE_NAME

> WHERE (BIT_MASK AND 3)

>

> That would return the line if BIT_MASK had bit 1 or 2 set.

>

> WHen I process the field, I am just using a regular integer in

>out.

> I am using the latest EMS interbase/Firebird manager to test this

>

> Is this possible?