位掩码问题? [英] bitmask question?

查看:113
本文介绍了位掩码问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下内容:

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

and I wanted to understand how it is calculated to give the follow result, for example: 12414

我真的很不知道如何使用位掩码工作,如果任何人可能会提供一些提示并解释它是如何进入这个数字我会非常感激很多。

I am really clueless on how the bitmask work and if any one could perhaps give some tips and explain how it goes to that number I would appreciate very much.

推荐答案

表达式(1<< n< n)等于2增加到n的幂。

The expression (1 << n) is equivalent to 2 raised to the power of n.

当你写(1<< n)| (1 这与相同(1<< n)+(1<<<<<<">只要 n m 不同。因此,如果您愿意,可以根据简单的添加情况来考虑它。

When you write (1 << n) | (1 << m) this is the same as (1 << n) + (1 << m) as long as n and m are different. So you can think of it in terms of simple additions if you wish.

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

The number 12414 in binary is 11000001111110 so it is the sum (or bitwise OR) of the following flags:


TRADEABLE                1 << 1  =     2
SELLABLE                 1 << 2  =     4
STORABLE                 1 << 3  =     8
STORABLE_IN_WH           1 << 4  =    16
STORABLE_IN_LEGION_WH    1 << 5  =    32
BREAKABLE                1 << 6  =    64
BLACK_CLOUD_TRADERS      1 << 12 =  4096
CAN_SPLIT                1 << 13 =  8192
========================================
                         Total   = 12414

请注意,包含的标志对应于12414的二进制表示中设置的位,当读取权限时-to-left。

Note that the flags that are included correspond to the bits that are set in the binary representation of 12414, when read right-to-left.

这篇关于位掩码问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆