目录

Bit Operation

目录

状态只有2种,可以用1位二进制(0,1)表示。n种这种状态可以用n位表示。

  • 节省空间
  • 扩展性强
  • 增加理解复杂度

在java中,int数据底层以补码形式存储。int型变量使用32bit存储数据,其中最高位是符号位,0表示正数,1表示负数,可通过Integer.toBinaryString()转换为bit字符串

1
2
3
4
5
6
// 若最高的几位为0则不输出这几位,从为1的那一位开始输出
System.out.println(Integer.toBinaryString(10)); 
System.out.println(Integer.toBinaryString(-10));
// 会输出(手工排版过,以下的输出均会被手工排版):
                            1010
11111111111111111111111111110110

左移和右移代替乘除

1
2
3
4
5
a=a*4;
b=b/4;
// 可以改为
a=a<<2;
b=b>>2;
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public final static Long OP_BIND_PHONE = 1L << 0;
public final static Long OP_BIND_EMAIL = 1L << 1;
public final static Long OP_BASIC_INFO = 1L << 2;
public final static Long OP_BASIC_INFO = 1L << 3;

// states 状态值    value 需要判断的状态 上面的值
public static boolean hasState(long states, long value) {
  return (states & value) != 0;
}
// 添加状态
public static boolean addState(long states, long value) {
  if (hasState(states, value)) {
    return states;
  }
  return states | value;
}
// 删除状态
public static boolean removeState(long states, long value) {
  if (!hasState(states, value)) {
    return states;
  }
  return states ^ value;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
public class BitStatusUtils {

    //A:语文 B:数学 C:英语 D:物理 E:化学 F:生物 G:历史 H:地理 

    // 二进制表示 0001 没有交任何作业
    public static final int NONE              = 1<<0;   //默认
    public static final int CHINESE           = NONE<<1;//语文
    public static final int MATH              = NONE<<2;//数学
    public static final int ENGLISH           = NONE<<3;//英语
    public static final int PHYSICS           = NONE<<4;//物理
    public static final int CHEMISTRY         = NONE<<5;//化学
    public static final int BIOLOGY           = NONE<<6;//生物
    public static final int HISTORY           = NONE<<7;//历史
    public static final int GEOGRAPHY         = NONE<<8;//地理

    public static final int ALL =NONE|CHINESE|MATH|ENGLISH|PHYSICS|CHEMISTRY|BIOLOGY|HISTORY|GEOGRAPHY;

    /**
     * @param status 所有状态值
     * @param value  需要判断状态值
     * @return 是否存在
     */
    public static boolean hasStatus(long status, long value) {
        return (status & value) != 0;
    }

    /**
     * @param status 已有状态值
     * @param value  需要添加状态值
     * @return 新的状态值
     */
    public static long addStatus(long status, long value) {
        if (hasStatus(status, value)) {
            return status;
        }
        return (status | value);
    }

    /**
     * @param status 已有状态值
     * @param value  需要删除状态值
     * @return 新的状态值
     */
    public static long removeStatus(long status, long value) {
        if (!hasStatus(status, value)) {
            return status;
        }
        return status ^ value;
    }
    
     /**是否交了含有全部状态
     * @param status 
     * @return
     */
    public static boolean hasAllStatus(long status) {
        return (status & ALL) == ALL;
    }

    public static void main(String[] args) {

        long status = addStatus(NONE, CHINESE);
        System.out.println("小明交了语文作业:" + status);

        status = addStatus(status, PHYSICS);
        System.out.println("小明又交了物理作业:" + status);

        status = addStatus(status, HISTORY);
        System.out.println("小明还交了历史作业:" + status);

        status = removeStatus(status, HISTORY);
        System.out.println("小明撤销了历史作业:" + status);

        System.out.println("小明是否交了语文作业:" + hasStatus(status, CHINESE));
        System.out.println("小明是否交了历史作业:" + hasStatus(status, HISTORY));
        System.out.println("小明是否交了生物作业:" + hasStatus(status, BIOLOGY));
        System.out.println("小明是否交了全部作业:" + hasAllStatus(status));
    }
}