位运算

位运算符比一般的算术运算符速度要快,而且可以实现一些算术运算符不能实现的功能。如果要开发高效率程序,位运算符是必不可少的。位运算符用来对二进制位进行操作,包括:按位与(&)、按位或(|)、按位异或(^)、按位取反(~)、按位左移(<<)、按位右移(>>)。下面就给大家介绍位运算符的详细用法。

一、按位与 and(&)

对两个数进行操作,然后返回一个新的数,这个数的每个位都需要两个输入数的同一位都为1时才为1,如下:

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

00101

11100

----------------

00100

二、按位或 or(|)

比较两个数,然后返回一个新的数,这个数的每一位设置1的条件是两个输入数的同一位都不为0(即任意一个为1,或都为1),如下:

相同位只要一个为1即为1。

00101

11100

----------------

11101

三、按位异或 xor(^)

比较两个数,然后返回一个数,这个数的每个位设为1的条件是两个输入数的同一位不同,如果相同就设为0,

xor运算的逆运算是它本身,也就是说两次异或同一个数最后结果不变,即(a xor b) xor b = a。xor运算可以用于简单的加密,比如我想对我MM说1314520,但怕别人知道,于是双方约定拿我的生日19880516作为密钥。1314520 xor 19880516 = 20665500,我就把20665500告诉MM。MM再次计算20665500 xor 19880516的值,得到1314520。

如下:

00101

11100

----------------

11001

四、按位取反 not(~)

对一个操作数的每一位都取反,如下:

00101

----------------

11010

五、按位左移 shl(<<)

将操作数的所有位向左移动指定的位数,右末位补0。如下左移一位:

00101

----------------

01010

六、按位右移 shr(<<)

将操作数的所有位向又移动指定的位数,左末位补0.如下右移一位:

00101

----------------

00010

七、优先级

C语言中位运算符之间,按优先级顺序排列为

1 ~

2 <<、>>

3 &

4 ^

5 |

6 &=、^=、|=、<<=、>>=

八、iOS中的应用,在iOS中NS_OPTION就是用利用位运算来实现的。举例说明:

typedefNS_OPTIONS(NSUInteger, SDWebImageOptions) {

    /**

     * By default, when a URL fail to be downloaded, the URL is blacklisted so the library won't keep trying.

     * This flag disable this blacklisting.

     */

    SDWebImageRetryFailed =1<<0,

    /**

     * By default, image downloads are started during UI interactions, this flags disable this feature,

     * leading to delayed download on UIScrollView deceleration for instance.

     */

    SDWebImageLowPriority =1<<1,

    /**

     * This flag disables on-disk caching

     */

    SDWebImageCacheMemoryOnly =1<<2,

    /**

     * This flag enables progressive download, the image is displayed progressively during download as a browser would do.

     * By default, the image is only displayed once completely downloaded.

     */

    SDWebImageProgressiveDownload =1<<3,

    /**

     * Even if the image is cached, respect the HTTP response cache control, and refresh the image from remote location if needed.

     * The disk caching will be handled by NSURLCache instead of SDWebImage leading to slight performance degradation.

     * This option helps deal with images changing behind the same request URL, e.g. Facebook graph api profile pics.

     * If a cached image is refreshed, the completion block is called once with the cached image and again with the final image.

     *

     * Use this flag only if you can't make your URLs static with embedded cache busting parameter.

     */

    SDWebImageRefreshCached =1<<4,

    /**

     * In iOS 4+, continue the download of the image if the app goes to background. This is achieved by asking the system for

     * extra time in background to let the request finish. If the background task expires the operation will be cancelled.

     */

    SDWebImageContinueInBackground =1<<5,

    /**

     * Handles cookies stored in NSHTTPCookieStore by setting

     * NSMutableURLRequest.HTTPShouldHandleCookies = YES;

     */

    SDWebImageHandleCookies =1<<6,

    /**

     * Enable to allow untrusted SSL certificates.

     * Useful for testing purposes. Use with caution in production.

     */

    SDWebImageAllowInvalidSSLCertificates =1<<7,

    /**

     * By default, images are loaded in the order in which they were queued. This flag moves them to

     * the front of the queue.

     */

    SDWebImageHighPriority =1<<8,


    /**

     * By default, placeholder images are loaded while the image is loading. This flag will delay the loading

     * of the placeholder image until after the image has finished loading.

     */

    SDWebImageDelayPlaceholder =1<<9,

    /**

     * We usually don't call transformDownloadedImage delegate method on animated images,

     * as most transformation code would mangle it.

     * Use this flag to transform them anyway.

     */

    SDWebImageTransformAnimatedImage =1<<10,


    /**

     * By default, image is added to the imageView after download. But in some cases, we want to

     * have the hand before setting the image (apply a filter or add it with cross-fade animation for instance)

     * Use this flag if you want to manually set the image in the completion when success

     */

    SDWebImageAvoidAutoSetImage =1<<11

};

传值的时候可以用按位或(|)来传多个值,如,options = SDWebImageRetryFailed|SDWebImageLowPriority|SDWebImageCacheMemoryOnly,判断的时候再进行一次按位与(&)运算,判断是否为真,如,options&SDWebImageRetryFailed,判断上述 值为真就可以了

你可能感兴趣的:(位运算)