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