BitMask operation in java

Consider the scenario I have values ​​assigned like these

Amazon -1

Walmart -2

Goal -4

Costco -8

Bjs -16

Data is stored in the database by masking these values ​​depending on their availability for each product. eg.,

Mask product description

1 laptop Available on Amazon

17 iPhone Available on Amazon and BJ

24 Mattress Available at Costco and BJ's

Similarly, all products are masked and stored in the database.

How to get all retailers based on Masked values. for example, for a mattress, the masking value is 24. Then how can I find or list Costco and BJ programmatically. Any algorithm / logic would be highly appreciated.

+7
java bitmask
source share
4 answers
int mattress = 24; int mask = 1; for(int i = 0; i < num_stores; ++i) { if(mask & mattress != 0) { System.out.println("Store "+i+" has mattresses!"); } mask = mask << 1; } 

The if builds bits if the mattress value has the same bit as the installed mask, then the store whose mask sells the mattresses. Both the mattress value and the mask value will only be non-zero when the store sells mattresses. For each iteration, we move the mask bit one position to the left.

Note that mask values ​​should be positive, not negative, if necessary, you can multiply by negative.

+9
source share

Assuming what you mean in the SQL database, then in your search SQL you can usually add, for example. WHERE (MyField AND 16) = 16, WHERE (MyField AND 24) = 24, etc.

However, please note that if you are trying to optimize such search options, and the number of rows that usually match the query is much less than the total number of rows, then this is probably not a good way to present this data. In this case, it would be better to have a separate table "ProductStore" that contains pairs (ProductID, StoreID) representing this information (and indexed to StoreID).

+1
source share

Are there no more than two retailers whose stocks are summed with the “masked” value in each case? If so, you still have to check all the pairs to get them, which takes n² time. Just use a nested loop.

If the value is the sum of any number of retailers' inventories, then you are trying to solve the subset-sum problem, so unfortunately you cannot do it better than 2 ^ n time.

If you can increase the original data structure with the help of information to find sellers contributing to the amount, then this would be ideal. But since you are asking a question, I assume that you do not have access to the data structure at the time of its creation, so to create all the subsets of retailers for verification, you will need to look at the Knuth [pdf] algorithm to generate all k-combinations (and runs them for 1 ... k) given in TAOCP Vol 4a Sec 7.2.1.3.

+1
source share

http://www.antiifcampaign.com/

Remember this. If you can remove the "if" with a different construct (map / strategy template), for me you can allow it there, otherwise the "if" is really dangerous! (F.Cirillo)

In this case, you can use a card card with a bit mask.

Luca.

0
source share

All Articles