• Java 用标志位表示状态


    public class JavaFlag {
    
        //    private static int BRAND_
        private static int BRAND_NO_SET = 0; // 0
        private static int BRAND_A = 1 << 0; // 1
        private static int BRAND_B = 1 << 1; // 2
        private static int BRAND_C = 1 << 2; // 4
    
        public static int mBrand = BRAND_NO_SET;
    
        public static void main(String[] args) {
            System.out.println(BRAND_NO_SET);
            System.out.println(BRAND_A);
            System.out.println(BRAND_B);
            System.out.println(BRAND_C);
    
            JavaFlag javaflag = new JavaFlag();
            javaflag.addSubBrand(BRAND_B);
            javaflag.addSubBrand(BRAND_A);
            javaflag.addSubBrand(BRAND_C);
            System.err.println(javaflag.mBrand);
            System.err.println(javaflag.mBrand);
            System.out.println(javaflag.mBrand & BRAND_A);
        }
    
        private void addSubBrand(int brand) {
            mBrand |= brand;
            System.out.println("添加品牌标志位:" + mBrand);
        }
    
        private void removeSubBrand(int brand) {
            mBrand &= ~brand;
            System.out.println("移除品牌标志位:" + mBrand);
        }
    
        private boolean hasSubBrand(int brand) {
            return (mBrand & brand) != 0;
        }
    
        private boolean isJustAllow(int brand) {
            return mBrand == brand;
        }
    }

    将十进制输出为 二进制:

            System.out.println(Integer.toBinaryString(1 << 0));
            System.out.println(Integer.toBinaryString(1 << 1));
            System.out.println(Integer.toBinaryString(1 << 2));
            System.out.println(Integer.toBinaryString(1 << 3));
            System.out.println(Integer.toBinaryString(1 << 4));
            System.out.println(Integer.toBinaryString(1 << 5));
            System.out.println(Integer.toBinaryString(1 << 6));

    输出结果:

    1
    10
    100
    1000
    10000
    100000
    1000000

    https://blog.csdn.net/jdsjlzx/article/details/93977574

  • 相关阅读:
    Android_学习系列(33)--App应用之提交到各大市场渠道
    Android_TextView使用Spanable
    6 种CSS设置居中的方法
    如何设置Grunt
    C#中的Collection 3
    C#中的Collection 2
    C#中的Collection 1
    网页上的JS call Unity3d里的function——SendMessage
    关于WebPlayer Sandbox的小节
    完整Deploy WebPlayer的Config
  • 原文地址:https://www.cnblogs.com/onelikeone/p/14361692.html
Copyright © 2020-2023  润新知