<< operator in objective c enum?

2020-05-23 03:00发布

问题:

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 know the << from ruby but I don't really understand this enum ?

enum {
    UITableViewCellStateDefaultMask                     = 0,
    UITableViewCellStateShowingEditControlMask          = 1 << 0,
    UITableViewCellStateShowingDeleteConfirmationMask   = 1 << 1
};

Thanks

BTW Found it as a great way to learn coding, I am trying once in a day to get into the header files of at list on object.

Shani

回答1:

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.



回答2:

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



回答3:

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.



回答4:

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.



回答5:

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.



回答6:

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