<< operator in objective c enum?

后端 未结 6 1189
别跟我提以往
别跟我提以往 2021-02-04 05:46

I was looking for something and got in to this enum is apple UITableViewCell.h.

I am sorry if this is trivial but I wonder/curious what is the point of this.

I

相关标签:
6条回答
  • 2021-02-04 06:13

    Its actually BItwise shift operator

    <<  Indicates the bits are to be shifted to the left.
    >>  Indicates the bits are to be shifted to the right.
    

    So in your statement the value of 1 << 0 is 1 and 1 << 1 is 2

    0 讨论(0)
  • 2021-02-04 06:16

    It's a common trick in C to use the bitwise shift operator in enum values to allow you to combine enumeration values with the bitwise or operator.

    That piece of code is equivalent to

    enum {
        UITableViewCellStateDefaultMask                     = 0,
        UITableViewCellStateShowingEditControlMask          = 1, // 01 in binary
        UITableViewCellStateShowingDeleteConfirmationMask   = 2  // 10 in binary
    };
    

    This allows you to bitwise or two or more enumeration constants together

     (UITableViewCellStateShowingEditControlMask | UITableViewCellStateShowingDeleteConfirmationMask) // == 3 (or 11 in binary)
    

    to give a new constant that means both of those things at once. In this case, the cell is showing both an editing control and a delete confirmation control, or something like that.

    0 讨论(0)
  • 2021-02-04 06:19

    These types of operator are called bitwise operator which operates on bit value of a number. These operation are very fast as compared to other arithematic operations.

    0 讨论(0)
  • 2021-02-04 06:34

    That is the bitshift operator. That is used commonly for objects that may have multiple behaviors (each enum being a behavior).

    Here is a similar post that may clarify it better.

    0 讨论(0)
  • 2021-02-04 06:39

    These are bit-field flags. They are used because you can combine them using the bitwise-OR operator. So for example you can combine them like

    (UITableViewCellStateShowingEditControlMask | UITableViewCellStateShowingDeleteConfirmationMask)
    

    They work by having one bit set in an integer. In this example, in binary,

    UITableViewCellStateShowingEditControlMask        = 0000 0001
    UITableViewCellStateShowingDeleteConfirmationMask = 0000 0010
    

    When they are OR'ed together, they produce 0000 0011. The framework then knows that both of these flags are set.

    The << operator is a left-shift. It shifts the binary representation. So 1 << 1 means

    0000 0001 shifted left by one bit = 0000 0010
    

    1 << 2 would equal 0000 0100.

    0 讨论(0)
  • 2021-02-04 06:41

    These operand called bitshift. Bitshift operand can be preferred for 2 reasons. - For fast operation - Use multiple bool value in one time.

    For example : 1<<2 is a left shift; that means 1 : 0001, 2 : 0010

    1 << 2 this line means is 2 should be left one bit. As a result 0010 shifted to 0100

    Also shifted value must ordered as a 1,2,4,8,16...

    typedef NS_OPTIONS(int, EntityEngineer) {
    EntityEngineeriOS = 1 << 0,
    EntityCategoryAndroid = 1 << 1,
    EntityCategoryDB = 1 << 2,
    EntityCategoryTeamLead = 1 << 16,};
    

    Now, we want to check mutiple boolean in below line,

    char engineer = (EntityEngineeriOS | EntityCategoryAndroid);
    

    char 0011 = (0001 | 0010);

    if (engineer & EntityEngineeriOS) {
        NSLog(@"we are looking for a developer who can write objective c or java");
    }
    
    if (engineer & EntityCategoryDB) {
        NSLog(@"we are not looking for a DB manager");
    }
    

    Result : we are looking for a developer who can write objective c or java

    0 讨论(0)
提交回复
热议问题