小编典典

位掩码问题?

java

我有以下几点:

public static final int LIMIT_ONE = 1;
public static final int TRADEABLE = (1 << 1);
public static final int SELLABLE = (1 << 2);
public static final int STORABLE = (1 << 3);
public static final int STORABLE_IN_WH = (1 << 4);
public static final int STORABLE_IN_LEGION_WH = (1 << 5);
public static final int BREAKABLE = (1 << 6);
public static final int SOUL_BOUND = (1 << 7);
public static final int UNK9 = (1 << 8);
public static final int UNK10 = (1 << 9);
public static final int UNK11 = (1 << 10);
public static final int CAN_COMPOSITE_WEAPON = (1 << 11);
public static final int BLACK_CLOUD_TRADERS = (1 << 12);
public static final int CAN_SPLIT = (1 << 13);
public static final int UNK15 = (1 << 14);
public static final int UNK16 = (1 << 15);

我想了解如何计算得出以下结果,例如: 12414

我对位掩码的工作原理一无所知,如果有人能给出一些提示并解释它如何达到这个数字,我将不胜感激。


阅读 240

收藏
2020-12-03

共1个答案

小编典典

该表达式(1 << n)等效于2的n次幂。

您撰写(1 << n) | (1 << m)本文时(1 << n) + (1 << m),只要n和相同,m就不同。因此,您可以根据需要以简单的添加方式来考虑它。

12414以二进制是11000001111110所以它是下列标志的总和(或按位OR):

交易1 << 1 = 2
卖1 << 2 = 4
可存储1 << 3 = 8
STORABLE_IN_WH 1 << 4 = 16
STORABLE_IN_LEGION_WH 1 << 5 = 32
易碎1 << 6 = 64
BLACK_CLOUD_TRADERS 1 << 12 = 4096
CAN_SPLIT 1 << 13 = 8192
=======================================
                         总计= 12414

请注意,当从右到左读取时,包含的标志对应于在12414的二进制表示形式中设置的位。

2020-12-03