java中的BitMask操作

发布于 2024-08-23 19:49:16 字数 449 浏览 10 评论 0原文

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

亚马逊-1

沃尔玛-2

目标-4

好市多-8

北京-16

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

面膜产品说明

1 台笔记本电脑在亚马逊有售

17 iPhone 在亚马逊有售 和北京

24 款床垫可供选择 Costco 和 BJ's

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

如何根据屏蔽值检索所有零售商。, 例如,对于床垫,屏蔽值为 24。那么我如何找到或列出 Costco & 床垫? BJ 以编程方式。任何算法/逻辑都将受到高度赞赏。

Consider the scenario
I have values assigned like these

Amazon -1

Walmart -2

Target -4

Costco -8

Bjs -16

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

Mask product description

1 laptop Available in Amazon

17 iPhone Available in Amazon
and BJ

24 Mattress Available in
Costco and 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

黑白记忆 2024-08-30 19:49:16
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;
}

if 语句将各个位对齐,如果床垫值与掩码集具有相同的位,则掩码为该商店的商店出售床垫。仅当商店出售床垫时,床垫值和掩码值的 AND 才会为非零。对于每次迭代,我们将掩码位向左移动一个位置。

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

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 statement 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.

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

别念他 2024-08-30 19:49:16

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

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

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.

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).

£冰雨忧蓝° 2024-08-30 19:49:16

在每种情况下,最多有两家零售商的库存总和等于“掩盖”价值吗?如果是这样,您仍然需要检查所有对来检索它们,这将花费 n² 时间。只需使用嵌套循环即可。

如果该值代表任意数量的零售商库存的总和,那么您正在尝试解决子集-sum 问题,所以不幸的是你不能在超过 2^n 的时间内完成它。

如果您能够使用信息来增强原始数据结构,以查找对总金额有贡献的零售商,那么这将是理想的选择。但既然你问这个问题,我假设你在构建数据结构时无法访问数据结构,因此要生成零售商的所有子集进行检查,你将需要查看 Knuth 算法 [pdf] 用于生成所有 k 组合(并运行 1...k)在 TAOCP 第 4a 卷第 7.2.1.3 节中给出。

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 n² time. Just use a nested loop.

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

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 TAOCP Vol 4a Sec 7.2.1.3.

挽心 2024-08-30 19:49:16

http://www.antiifcampaign.com/

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

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

卢卡.

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)

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

Luca.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文