Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 644
  • Last Modified:

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:

00001000011100000001000000100001 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
0
PaultheBroker
Asked:
PaultheBroker
  • 3
  • 3
  • 2
  • +2
1 Solution
 
domdornCommented:
Which Database do you use?
0
 
js-profiCommented:
you better store a redundant bitcount field with each bitmask. can't be done efficiently.
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
what data type is the field for the bitmask?
if it is varchar, just do this:
len(replace(yourfield, '0',''))

if it's not varchar, just cast it to varchar, and apply the above expression
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
PaultheBrokerAuthor Commented:
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(myBitMask)  as varchar2), '0',''))

Haven't got access to my oracle installation right now, so would appreciate someone confirming that that would work....
0
 
slightwv (䄆 Netminder) Commented:
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).
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
> 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!
0
 
slightwv (䄆 Netminder) Commented:
>>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.  ;)
0
 
PaultheBrokerAuthor Commented:
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...
0
 
slightwv (䄆 Netminder) Commented:
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?
0
 
PaultheBrokerAuthor Commented:
Thanks !!  I'll open another question to ask how to store a VLN (very large number!)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now