java java中的位掩码操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2353254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 20:44:46  来源:igfitidea点击:

BitMask operation in java

javabitmask

提问by Anand

Consider the scenario I have values assigned like these

考虑我分配了这样的值的场景

Amazon -1

Walmart -2

Target -4

Costco -8

Bjs -16

亚马逊-1

沃尔玛-2

目标 -4

好市多 -8

北京-16

In DB, data is stored by masking these values based on their availability for each product. eg.,

在 DB 中,通过根据每个产品的可用性屏蔽这些值来存储数据。例如。,

Mask product description

1 laptop Available in Amazon

17 iPhone Available in Amazon and BJ

24 Mattress Available in Costco and BJ's

面膜产品说明

亚马逊有 1 台笔记本电脑

17 iPhone 在亚马逊和 BJ 有售

24 张床垫可在 Costco 和 BJ's 购买

Like these all the products are masked and stored in the DB.

像这些一样,所有产品都被屏蔽并存储在数据库中。

How do I retrieve all the Retailers based on the Masked value., eg., For Mattress the masked value is 24. Then how would I find or list Costco & BJ's programmatically. Any algorithm/logic would be highly appreciated.

我如何根据掩码值检索所有零售商。例如,对于床垫,掩码值为 24。那么我将如何以编程方式查找或列出 Costco & BJ。任何算法/逻辑将不胜感激。

回答by David Kanarek

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 ifstatement lines up the the bits, if the mattress value has the same bit as the mask set, then the store whose mask that is sells mattresses. An AND of the mattress value and 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.

if语句将这些位对齐,如果床垫值与口罩组具有相同的位,则该口罩所在的商店出售床垫。只有当商店出售床垫时,床垫值和面罩值的 AND 才会非零。对于每次迭代,我们将掩码位向左移动一个位置。

Note that the mask values should be positive, not negative, if need be you can multiply by negative one.

请注意,掩码值应为正数,而不是负数,如果需要,您可以乘以负数。

回答by Neil Coffey

Assuming you mean in a SQL database, then in your retrieval SQL, you can generally add e.g. WHERE (MyField AND 16) = 16, WHERE (MyField AND 24) = 24 etc.

假设您的意思是在 SQL 数据库中,那么在检索 SQL 中,您通常可以添加例如 WHERE (MyField AND 16) = 16、WHERE (MyField AND 24) = 24 等。

However, note that if you're trying to optimise such retrievals, and the number of rows typically matching a query is much smaller than the total number of rows, then this probably isn't a very good way to represent this data. In that case, it would be better to have a separate "ProductStore" table that contains (ProductID, StoreID) pairs representing this information (and indexed on StoreID).

但是,请注意,如果您正在尝试优化此类检索,并且通常与查询匹配的行数远小于总行数,那么这可能不是表示这些数据的好方法。在这种情况下,最好有一个单独的“ProductStore”表,其中包含表示此信息的 (ProductID, StoreID) 对(并在 StoreID 上建立索引)。

回答by Imran

Are there at most two retailers whose inventories sum to the "masked" value in each case? If so you will still have to check all pairs to retrieve them, which will take n2 time. Just use a nested loop.

在每种情况下,是否至多有两个零售商的库存总和为“掩码”值?如果是这样,您仍然需要检查所有对以检索它们,这将花费 n2 时间。只需使用嵌套循环。

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

如果该值代表任意数量零售商库存的总和,那么您正在尝试解决子集总和问题,因此不幸的是,您无法在 2^n 时间内完成。

If you are able to augment your original data structure with information to lookup the retailers contributing to the sum, then this would be ideal. But since you are asking the question I am assuming you don't have access to the data structure while it is being built, so to generate all subsets of retailers for checking you will want to look into Knuth's algorithm[pdf] for generating all k-combinations (and run it for 1...k) given in TAOCPVol 4a Sec 7.2.1.3.

如果您能够使用信息来扩充您的原始数据结构以查找对总和做出贡献的零售商,那么这将是理想的。但是既然你问这个问题,我假设你在构建数据结构时无权访问它,所以要生成零售商的所有子集进行检查,你需要研究Knuth 的算法[pdf] 来生成所有 k -TAOCPVol 4a Sec 7.2.1.3 中给出的组合(并运行 1...k)。

回答by neuromancer977

http://www.antiifcampaign.com/

http://www.antiifcampaign.com/

Remember this. If you can remove the "if" with another construct(map/strategy pattern), for me you can let it there, otherwise that "if" is really dangerous!! (F.Cirillo)

记住这一点。如果你可以用另一个构造(地图/策略模式)删除“if”,对我来说你可以让它在那里,否则那个“if”真的很危险!!(F.Cirillo)

In this case you can use map of map with bitmask operation.

在这种情况下,您可以使用带有位掩码操作的地图映射。

Luca.

卢卡。