# -*- coding: utf-8 -*-
"""
二进制状态标识
"""

# 是否包含状态
def hasState(states, operateState):
    return (states & operateState) != 0

# 添加状态
def addState(states, operateState):
    if (hasState(states, operateState)):
        return states;
    return (states | operateState)

# 删除状态
def removeState(states, operateState):
    if (not hasState(states, operateState)):
        return states
    return states ^ operateState


FIRST_STATE = 1L << 0  # 0000 0001
SECOND_STATE = 1L << 1  # 0000 0010
THIRD_STATE = 1L << 2  # 0000 0100
FOURTH_STATE = 1L << 3  # 第四个状态  0000 1000
FIFTH_STATE = 1L << 4  # 第五个状态   0001 0000
SIXTH_STATE = 1L << 5  # 第六个状态   0010 0000
SEVENTH_STATE = 1L << 6  # 第七个状态 0100 0000
EIGHTH_STATE = 1L << 7  # 第八个状态  1000 0000

s = 0
s = addState(s, FIRST_STATE)
print s
s = addState(s, FIFTH_STATE)
print s
s = addState(s, SEVENTH_STATE)
print s
print hasState(s, FIRST_STATE)
print hasState(s, SECOND_STATE)
print hasState(s, THIRD_STATE)
print hasState(s, FOURTH_STATE)
print hasState(s, FIFTH_STATE)
print hasState(s, SIXTH_STATE)
print hasState(s, SEVENTH_STATE)
print hasState(s, EIGHTH_STATE)

09-27 05:32