directory

1. Introduction

2. Bit operators

3. Bit operator application

preface

In the process of our development program, the computer doesn’t actually know we write the code, we usually write code by the compiler eventually converted to 0/1 of the binary file to the computer, and an operation is directly manipulate these binary data, an operation is a very efficient and arguably the most efficient way of computing, Although modern compilers have made a lot of optimizations for us, the proper use of bit operations can improve the readability and execution efficiency of the code. IOS also uses many scenarios such as enumeration, color storage, etc.

An operator

An operator there are the following four 1. & bitwise and (2) | bitwise or (3) ^ bitwise exclusive or 4. To take the 5. < < left 6. > > moves to the right

= = = = = = = = = = = = = = = = = Convert it to binary 7:0111 1:0001 according to the rules above the result is: 0001 is 1 so 7&1 = 1

| bitwise or rule: as long as there is 1 to 1:7 | 1 value is how much? Convert them into binary 7:1:0111, 0001, in accordance with the rules of the above results as follows: 0111 for 7 so 7 | 1 = 7

^ What is the value of 7 ^ 1? 7: 0111 1: 0001 The result is: 0110 is 6 so 7 ^ 1 = 6

Rule 2: The result of xOR has no relation to the order in which the operation is performed. For example, the result of 9 ^ 5 ^ 6 and 5 ^ 6 ^ 9 is the same: 9:1001 5:0101 -> 1100 6:0110 Result: 1010 == 10 5:0101 6: 0110 -> 0011 9: 1001 Result: 1010 == 10

Rule 3: Two identical numbers are different or equal to 0 e.g. 5 ^ 5 5:0101 5:0101 result: 0000 == 0

Rule 4: The result of any number is the same as 5 ^ 0 5:0101 0:0000 result: 0101 == 5

Rule 5: any number of xor the same number twice, the result is the same as 9 ^ 5 ^ 5\9:1001 5:0101 -> 1100 5:0101 result: 1001 == 9

Rule 2: The bitwise of a negative integer is the absolute value of its own +1, that is, ~ -9 = 8. Rule 3: the bitwise of zero is -1, that is, ~ 0 = -1

<< left shift format: integer << Number of left shifts Example: x << n Substantial: x * 2N Operation: move the binary bits of x to the left by n units, discard the high bits, fill the low bits with 0 for example: 1 << 2 1: 0001 << 0100 Result: 4

<< right shift format: integer >> Number of right shifts Example: X >> N Substance: x / 2N (2 to the n) operation: move the binary bit of x to the right n units, low discard, sign bit unchanged common enumeration such as: 8 >> 1 8:0100 ·>> 0010 result: 4


Bitwise operator application

  • In OC, we often see the system define enumerations, many of which look like this:

  1. Let’s implement an enumeration ourselves using bits:
typedef enum {
     OptionsNone = 0.// 0b0000
     OptionsOne = 1<<0.// 0b0001
     OptionsTwo = 1<<1.// 0b0010
     OptionsThree = 1<<2.// 0b0100
     OptionsFour = 1<<3   // 0b1000
} Options; 

- (void)setOptions:(Options)options
{
    if (options & OptionsOne) {
        NSLog(@"Contains OptionsOne.");
    }
    
    if (options & OptionsTwo) {
        NSLog(@"Contains OptionsTwo.");
    }
    
    if (options & OptionsThree) {
        NSLog(@"Contains OptionsThree.");
    }
    
    if (options & OptionsFour) {
        NSLog(@"Contains OptionsFour."); }}- (void)viewDidLoad {
    [super viewDidLoad];
   // Set OptionsOne and OptionsFour
   [self setOptions:  OptionsOne |  OptionsFour];
   // Print: contains OptionsOne, contains OptionsFour
}
Copy the code

Through the above code we analyze why call [the self setOptions: OptionsOne | OptionsFour] this method will print setup parameters; First of all, through the above study we know: the value is 0 b0001 OptionsOne (0 b binary), OptionsFour value is 0 b0100 so OptionsOne | OptionsFour is 0001 | 0100 results as follows: 0101 so (options & OptionsOne) = 0101&0001 result 0001 value 1 (greater than 0, Bool YES), Print the content so (options & OptionsFour) = 0101&0100 the result is 0100 and the value is 4, YES, print the content


  1. Let’s take another look at LeetCode: leetcode-cn.com/problems/si…

Given an array of non-empty integers, each element appears twice except for one element. Find the element that appears only once. Example 1: input: [2, 2, 1] output: 1 example 2: input:,1,2,1,2 [4] output: 4

Through our study of counterpoint operations, is it found that using ^ can easily solve the review of ^ several laws:

  • The xOR result has nothing to do with the order of participation
  • The same two numbers are xor equal to 0
  • I don't change any of these numbers
   func singleNumber(_ nums: [Int]) -> Int {
       var value = 0
       for num in nums{
         value = value ^ num
       }
       return value
    }
Copy the code