android 二进制的应用

在处理一些状态类型的参数时用二进制来优化内存。(消耗cpu)

  • 或运算符整合值
  • 与运算符取值
  • 与非剔除值

eg: private static final int a=0x0001;//0001

    private static final int b=0x0002;//0010

    private static final int c=0x0004;//0100

    private static final int d=0;//0000

    private static final int status=0;

   

  • 或运算符整合值

    private void setStatus(int status){

       this.status | =status;//eg : status= a|b (0011)

      }

   

  • 与运算符取值

   private boolean isContain(int status){

     return  (this.status & status) != 0; eg: a&this.status 即 0001&0011 为 0001  c&this.status 即 0100&0011 为 0000  

    }

  • 与非剔除值

   private void clear(int status){

       this.status &=~status;eg: this.status & = ~ a 即 ~a=1110  1110&0011 为 0010(b)清除a

     }

猜你喜欢

转载自www.cnblogs.com/wangandroid/p/10405481.html