|
-
Jul 8th, 2011, 05:31 PM
#1
Bitmasking too many values
Hi,
I am writing an administration tool for a game. I have a list of weapons in the game, and each weapon has a unique Id (numeric). Until recently I had this Id just 0, 1, 2, 3, etc...
Now however I need some way to allow the user to restrict use of some weapons, and store that information in an Access database. The most obvious approach I thought was to give the weapons Ids that were powers of two: 1, 2, 4, 8, 16, etc... This way, a user can check a number of weapons (via checkboxes or whatever), and I simply store the sum of their Ids in the database. Then I can use a bitmask and the bitwise And operator to check which weapons are restricted and which aren't.
Example: the user selects weapons with Id 2, 4 and 16. The database then stores the value 2+4+16 = 22.
Later, I come around and want to check if some weapon is restricted. I then take the bitwise AND operator of 22 and the Id of that weapon. If the result is the Id of the weapon, that means the weapon is restricted. Just the usual bitmasking...
Suppose the weapon Id is 8 then we have: 22 And 8 = 0. Nope, weapon 8 is not restricted!
Another weapon with Id 4 then: 22 And 4 = 4. Yep, weapon 4 is restricted!
Anyway... Long story short: that won't work for the simple reason that I have too many weapons! There are 81 weapons (and counting...) if I counted correctly, which means I'd need values up to 2^81. As far as I know no integer value type in .NET supports numbers that high, except perhaps the BigInteger. But remember: I have to store those Ids in the database as well (theoretically even up to 2^82 - 1 if the user decides to restrict every weapon), and I don't think an Access database supports anything over 2^64 (long)...?
Can anyone come up with a solution to this problem? Am I overlooking obvious solutions?
I did come up with one possible solution, but don't really like it. I could put weapons in their own 'category' (there are logical divisions already so that's easy) and start the count from 1 in each category. Then there would be no more then 15-20 weapons per category so those numbers are not too large (2^20). However then I'd have to store the restricted weapons for each category separately (resulting in quite a large table, there would be about 10-12 categories). I also need to store single weapons in another table, in that case I'd also have to add a column that stores the category of that weapon.
All in all, a lot of cumbersome work arounds that might not be necessary...
Anything else I can do to avoid having to work with numbers up to 2^82..?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|