Kayser
Kayser

Reputation: 6694

How to do bitwise operations in DB2

I try to find out which bit is set using BITAND.

In database I save the user rights as BIGINT like 1101111 or 1110001.

in the sample case I must find out the elemnts in the table if the third bit is set. In this case. The query should find the first one.

How can I do that??

Select * from USERRIGHTS
WHERE BITAND(Rightmask , 4) = 1 ??????

Upvotes: 1

Views: 4370

Answers (2)

Kayser
Kayser

Reputation: 6694

I found a solution. It was silly to save the right mask in this form '1101111'. I should save the data in decimal format. In this case 1101111 = 111 (in decimal) or 1110001 = 113. I converted in my application the right mask into decimla an saved in that format.

And then it was possible to check if a bit position is set or not.

This query gives the all entries what i want :-):

Select * from USERRIGHTS WHERE BITAND(Rightmask , 16) = 16 (The third bit position is the fifth bit position from the right. This value should be used for the bitwise operations. 2^(5-1) = 16)

Upvotes: 1

Fred Sobotka
Fred Sobotka

Reputation: 5332

On Linux, UNIX, and Windows DB2 platforms, bitwise operators were introduced in version 9.5.

Upvotes: 0

Related Questions