java中通过位运算实现多个状态的判断
2014-08-31 22:51
281 查看
通过 << | & ~ 位运算,实现同时拥有多个状态
通过 << 定义数据的状态
通过 |= 加入多个状态
通过 (m & LogConstants.COST_ASSET) > 0 判断是否拥有该状态
通过 m = (short) (m & ~LogConstants.COST_ASSET);扣除该状态
通过 << 定义数据的状态
public interface LogConstants { /** * 消耗标记 */ short COST_ASSET = 1 << 0; short COST_GOLD = 1 << 1; short COST_BINDGOLD = 1 << 2; short COST_SOPH = 1 << 3; short COST_STRSOUL = 1 << 4; short COST_REFSOUL = 1 << 5; short COST_SOULSTONE = 1 << 6; short COST_AAMHID = 1 << 7; short COST_REALM = 1 << 8; }
通过 |= 加入多个状态
short mark = 0; int costSoph = channel.getSoph(); if (costSoph > 0) { mark |= LogConstants.COST_SOPH; } if (costGold > 0) { mark |= LogConstants.COST_GOLD; }
通过 (m & LogConstants.COST_ASSET) > 0 判断是否拥有该状态
通过 m = (short) (m & ~LogConstants.COST_ASSET);扣除该状态
public void addCostLog(LogEvent event, Player player, short mark, int... args) {short m = mark; for (int i : args) { if ((m & LogConstants.COST_ASSET) > 0) { m = (short) (m & ~LogConstants.COST_ASSET); cost.setAsset(i); } else if ((m & LogConstants.COST_GOLD) > 0) { m = (short) (m & ~LogConstants.COST_GOLD); cost.setGold(i); } else if ((m & LogConstants.COST_BINDGOLD) > 0) { m = (short) (m & ~LogConstants.COST_BINDGOLD); cost.setBindGold(i); } else if ((m & LogConstants.COST_SOPH) > 0) { m = (short) (m & ~LogConstants.COST_SOPH); cost.setSoph(i); } else if ((m & LogConstants.COST_STRSOUL) > 0) { m = (short) (m & ~LogConstants.COST_STRSOUL); cost.setStrSoul(i); } else if ((m & LogConstants.COST_REFSOUL) > 0) { m = (short) (m & ~LogConstants.COST_REFSOUL); cost.setRefSoul(i); } else if ((m & LogConstants.COST_SOULSTONE) > 0) { m = (short) (m & ~LogConstants.COST_SOULSTONE); cost.setSoulStone(i); } else if ((m & LogConstants.COST_AAMHID) > 0) { m = (short) (m & ~LogConstants.COST_AAMHID); cost.setAamhid(i); } else if ((m & LogConstants.COST_REALM) > 0) { m = (short) (m & ~LogConstants.COST_REALM); cost.setRealm(i); } } }
相关文章推荐
- java中通过位运算实现多个状态的判断
- C#中通过位运算实现多个状态的判断
- java中把两个变量进行值交换,通过异或两次运算就能实现值互换。
- 一元多项式的乘法与加法运算【Java实现--通过单项链表实现队列】
- spring通过annotation注解注册MBean到JMX实现监控java运行状态
- 通过JS判断联网类型和连接状态的实现代码
- Java 位运算系列之通过位运算操作状态
- 通过JS判断联网类型和连接状态的实现代码
- 通过递归判断一个整形数组是否按升序排列(java实现)
- JAVA--第十周作业编写之一个Teacher类负责给出算术题目,随机给出两个整数并进行运算,并判断回答者的答案是否正确;编写一个GUI类ComputerFrame,回答者可以通过GUI看到题目并给出
- Java通过位运算实现10-16进制转换
- 通过JS判断联网类型和连接状态的实现代码
- spring通过annotation注解注册MBean到JMX实现监控java运行状态
- 通过select 和状态EINPROGRESS 实现socket 连接超时判断
- spring通过annotation注解注册MBean到JMX实现监控java运行状态
- java通过矩阵类实现矩阵的加减乘运算
- 一种通过GPS判断手机漫游状态实现自动搜索连接服务商的方法
- 3个参数通过两个判断语句实现3种状态
- 网络判断网络连接有多种办法,通过C#程序也可以判断与远程主机的连接状态。具体实现方法
- java数组实现买彩票(通过标识符进行判断的思想)