• 位运算实现多状态控制


      经常在源码阅读过程中,遇到一个状态标志位去标识多个状态,并对多状态进行控制的操作,下面是对多状态控制的一个总结:

    public class Main {
        public static final int SET_MOBILE = 0x1;
        public static final int SET_SMS_CODE = 0x2;
        public static final int SET_PASSWORD = 0x3;
        public static final int REGISTER = 0x4;
    
        public static Boolean isExistState(int currentSate, int targetSate) {
            return (currentSate & targetSate) == targetSate;
        }
    
        public static int cancelState(int currentSate, int targetSate) {
            return currentSate & ~targetSate;
        }
    
        public static void main(String[] args) {
            // 设置mRegisterState 目前有3个状态
            int mRegisterState = SET_MOBILE | SET_SMS_CODE | SET_PASSWORD;
            System.out.println("===========初始化状态,包含 SET_MOBILE  SET_SMS_CODE  SET_PASSWORD======================");
            System.out.println("存在状态SET_SMS_CODE : "+isExistState(mRegisterState, SET_SMS_CODE));
            System.out.println("存在状态REGISTER : "+isExistState(mRegisterState, REGISTER));
            System.out.println("存在状态SET_PASSWORD : "+isExistState(mRegisterState, SET_PASSWORD));
    
            System.out.println("=====================去除状态SET_SMS_CODE===========================");
            mRegisterState = cancelState(mRegisterState, SET_SMS_CODE);
            System.out.println("存在状态SET_SMS_CODE : "+isExistState(mRegisterState, SET_SMS_CODE));
            System.out.println("===========================去除状态SET_PASSWORD======================");
            mRegisterState &= ~SET_PASSWORD;
            System.out.println("存在状态SET_PASSWORD : "+isExistState(mRegisterState, SET_PASSWORD));
    
            System.out.println("===================添加状态REGISTER==================");
            mRegisterState |= REGISTER;
            System.out.println("存在状态REGISTER : "+isExistState(mRegisterState, REGISTER));
    
            System.out.println("===================“反转”操作:如果SET_SMS_CODE在组中,则从组中删除之;否则将SET_SMS_CODE加入组中==================");
            mRegisterState ^= SET_SMS_CODE;
            System.out.println("存在状态SET_SMS_CODE : "+isExistState(mRegisterState, SET_SMS_CODE));
    
            System.out.println("==================清空所有状态=======================");
            mRegisterState = 0;
            System.out.println("存在状态SET_SMS_CODE : "+isExistState(mRegisterState, SET_SMS_CODE));
            System.out.println("存在状态REGISTER : "+isExistState(mRegisterState, REGISTER));
            System.out.println("存在状态SET_PASSWORD : "+isExistState(mRegisterState, SET_PASSWORD));
            System.out.println("存在状态REGISTER : "+isExistState(mRegisterState, REGISTER));
    
            System.out.println("==================添加所有状态=======================");
            mRegisterState = ~0;
            System.out.println("存在状态SET_SMS_CODE : "+isExistState(mRegisterState, SET_SMS_CODE));
            System.out.println("存在状态REGISTER : "+isExistState(mRegisterState, REGISTER));
            System.out.println("存在状态SET_PASSWORD : "+isExistState(mRegisterState, SET_PASSWORD));
            System.out.println("存在状态REGISTER : "+isExistState(mRegisterState, REGISTER));
        }
    }
  • 相关阅读:
    把数据库转化成数据库脚本
    营养瘦身第一菜——金陵素什锦 健康程序员,至尚生活!
    十类好吃不胖的食物 健康程序员,至尚生活!
    一周带饭实录(附作菜菜谱) 健康程序员,至尚生活!
    日常五大习惯有助减肥 健康程序员,至尚生活!
    【暴强】200种好口碑便宜护肤品 健康程序员,至尚生活!
    腹式肠道操 缩胃瘦身有奇效 健康程序员,至尚生活!
    一天4时段喝水轻松瘦身 健康程序员,至尚生活!
    10种不要钱的护肤法则 健康程序员,至尚生活!
    看了这篇你肯定瘦 全身上下想瘦哪就瘦哪 健康程序员,至尚生活!
  • 原文地址:https://www.cnblogs.com/gaojy/p/14813690.html
Copyright © 2020-2023  润新知