位运算,安卓源码里的各种Flag

一直看不懂源码里的各种位运算,各种flag与非或非的,头大。

View里面有各种状态,都是用flag常量来标记的,然后用一个成员变量跟这些值通过位运算,来判断view当前的状态。

奈何在学校的时候没有好好学,各种知识都不记得了,今天重温Handler那块的源码时,突然有了灵感。

MessageQueue里面有个next方法,从消息队列中死循环不停取出消息,当然这个跟我要讲的内容无关,我贴出来是因为我是从这个方法中得到灵感的。

这个方法的22行有一句msg.markInUse();顾名思义,是调用msg的方法将该对象标记为正在使用。

    final Message next() {
        int pendingIdleHandlerCount = -1; // -1 only during first iteration
        int nextPollTimeoutMillis = 0;

        for (;;) {
            if (nextPollTimeoutMillis != 0) {
                Binder.flushPendingCommands();
            }
            nativePollOnce(mPtr, nextPollTimeoutMillis);

            synchronized (this) {
                // Try to retrieve the next message.  Return if found.
                final long now = SystemClock.uptimeMillis();
                final Message msg = mMessages;
                if (msg != null) {
                    final long when = msg.when;
                    if (now >= when) {
                        mBlocked = false;
                        mMessages = msg.next;
                        msg.next = null;
                        if (false) Log.v("MessageQueue", "Returning message: " + msg);
                        msg.markInUse();
                        return msg;
                    } else {
                        nextPollTimeoutMillis = (int) Math.min(when - now, Integer.MAX_VALUE);
                    }
                } else {
                    nextPollTimeoutMillis = -1;
                }

                // If first time, then get the number of idlers to run.
                if (pendingIdleHandlerCount < 0) {
                    pendingIdleHandlerCount = mIdleHandlers.size();
                }
                if (pendingIdleHandlerCount == 0) {
                    // No idle handlers to run.  Loop and wait some more.
                    mBlocked = true;
                    continue;
                }

                if (mPendingIdleHandlers == null) {
                    mPendingIdleHandlers = new IdleHandler[Math.max(pendingIdleHandlerCount, 4)];
                }
                mPendingIdleHandlers = mIdleHandlers.toArray(mPendingIdleHandlers);
            }

            // Run the idle handlers.
            // We only ever reach this code block during the first iteration.
            for (int i = 0; i < pendingIdleHandlerCount; i++) {
                final IdleHandler idler = mPendingIdleHandlers[i];
                mPendingIdleHandlers[i] = null; // release the reference to the handler

                boolean keep = false;
                try {
                    keep = idler.queueIdle();
                } catch (Throwable t) {
                    Log.wtf("MessageQueue", "IdleHandler threw exception", t);
                }

                if (!keep) {
                    synchronized (this) {
                        mIdleHandlers.remove(idler);
                    }
                }
            }

            // Reset the idle handler count to 0 so we do not run them again.
            pendingIdleHandlerCount = 0;

            // While calling an idle handler, a new message could have been delivered
            // so go back and look again for a pending message without waiting.
            nextPollTimeoutMillis = 0;
        }
    }


那么markInUse方法的内容又是什么呢?

    void markInUse() {
        flags |= FLAG_IN_USE;
    }

很简单的一句话,就是一句或运算。与之对应的获取该标记的方法是isInUse,方法如下:

    boolean isInUse() {
        return ((flags & FLAG_IN_USE) == FLAG_IN_USE);
    }


即判断 flags & FLAG_IN_USE 的结果是否等于FLAG_IN_USE,结果肯定是等于的。

为什么相等呢?先来看看位运算的基本知识。(感谢度娘带我回顾了一遍位运算。)


    运算符号

下面的a和b都是整数类型,则:
含义
  
Pascal语言 C语言 Java
按位与 a and b a & b a & b
按位或 a or b a | b a | b
按位异或 a xor b a ^ b a ^ b
按位取反 not a ~a ~a
左移 a shl b a << b a << b
带符号右移 a shr b a >> b a >> b
无符号右移

a>>> b


    运算说明

       and运算 

       相同位的两个数字都为1,则为1;若有一个不为1,则为0。

举例:  00101

                   11100
             ---------------
             00100

       or运算

相同位只要一个为1即为1。
       举例:  00101
                    11100
                    ----------------
                    11101

      xor运算

      相同位不同则为1,相同则为0。
      举例:  00101
                   11100
                   ----------------
                   11001

      not运算

      not运算的定义是把内存中的0和1全部取反。
      对于无符号整数:取反后的值就是它与该类型上界的差。 
      对于有符号整数:请找度娘

       shl运算

a shl b就表示把a转为二进制后左移b位(在后面添b个0)。例如100的二进制为1100100,而110010000转成十进制是400,那么100 shl 2 = 400。
可以看出,a shl b的值实际上就是a乘以2的b次方,因为在二进制数后添一个0就相当于该数乘以2。
常认为a shl 1比a * 2更快,因为前者是更底层一些的操作。因此程序中乘以2的操作请尽量用左移一位来代替。

     shr运算

和shl相似,a shr b表示二进制右移b位(去掉末b位),相当于a除以2的b次方(取整)。我们也经常用shr 1来代替div 2,比如二分查找、堆的插入操作等等。
想办法用shr代替除法运算可以使程序效率大大提高。最大公约数的二进制算法用除以2操作来代替慢得出奇的mod运算,效率可以提高60%。

优先级

C语言中位运算符之间,按优先级顺序排列为
1
~
2
<<、>>
3
&
4
^
5
|
6
&=、^=、|=、<<=、>>=


注:位运算都是基于二进制来进行计算的。


看到此处,对于聪明的你想必已经能够理解并能够轻松的计算安卓中的各种flag的各种位运算了。

另:有人可能会问,直接位运算而不考虑flags的初始值吗?flags不同的初始值会不会对结果产生不同的影响?

你可以自己用笔算下,flags不同初始值的情况下,这个结果是不是一样。(我算了好几遍是一样的,囧~)


这篇博文只是当作学习笔记之用,以后碰到合适的心得体会也会继续写在csdn里,源于知识层面受限,有些地方说的不一定对,原谅我只是个努力学习中的小菜鸟。


end。


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值