PaultheBroker
asked on
How do I count the number of set flags in a bitmask?
I know how to use bitwise operators to determine whether a particular bit is set or not. eg: to check if the third bit is set I might say (in psuedo code)
if myBitMask & 4 = 4 then TRUE
however, I have a bitmask of 2,000 bits, and i want count how many are set (the set bits represent a bad event occured, and i want to count how many bad events occurred in total.
eg:
00001000011100000001000000 100001 should be "7"
i.e in pseudo code: countofSetFlags(myBitMask)
I'm hoping to do this efficiently. I'll be querying over a million rows,
Thanks !!!
Paul
if myBitMask & 4 = 4 then TRUE
however, I have a bitmask of 2,000 bits, and i want count how many are set (the set bits represent a bad event occured, and i want to count how many bad events occurred in total.
eg:
00001000011100000001000000
i.e in pseudo code: countofSetFlags(myBitMask)
I'm hoping to do this efficiently. I'll be querying over a million rows,
Thanks !!!
Paul
Which Database do you use?
you better store a redundant bitcount field with each bitmask. can't be done efficiently.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I'm using oracle, and I was thinking of using datatype as LONG (as I think that will enable me to store a number as large as 2^2000 so looking at Angel's answer, I'm thinking that this might work:
SELECT len(replace(cast(dec2bin(m yBitMask) as varchar2), '0',''))
Haven't got access to my oracle installation right now, so would appreciate someone confirming that that would work....
SELECT len(replace(cast(dec2bin(m
Haven't got access to my oracle installation right now, so would appreciate someone confirming that that would work....
Use CLOB not LONG. They are much easier to work with then angel's solution will work with one small tweak. LENGTH instead of LEN (angel works with too many databases).
> LENGTH instead of LEN (angel works with too many databases).
actually, I didn't know that it shall be oracle so far :)=
thanks for the update!
actually, I didn't know that it shall be oracle so far :)=
thanks for the update!
>>I didn't know that it shall be oracle so far
DOH... good point. Oracle was mentioned AFTER your post. Your humble servant apologizes oh wise one. ;)
DOH... good point. Oracle was mentioned AFTER your post. Your humble servant apologizes oh wise one. ;)
ASKER
sorry, LONG is a text field, so now I'm stuck looking for a way to store a number 2^2000) or about 10^600...
RAW data can be stored in LONG RAW but I strongly suggest using LOBs. IN this case: BLOB.
Just a thought: It's been years since I messed in the binary world but could you not store the character representation of the bitmask in the database?
Just a thought: It's been years since I messed in the binary world but could you not store the character representation of the bitmask in the database?
ASKER
Thanks !! I'll open another question to ask how to store a VLN (very large number!)