17

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

shannoga
  • 19,649
  • 20
  • 104
  • 169

6 Answers6

28

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.

joerick
  • 16,078
  • 4
  • 53
  • 57
4

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

Ali3n
  • 1,244
  • 6
  • 10
  • 3
    You have copied this text from [IBM documentation](http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8l.doc%2Flanguage%2Fref%2Fbitshe.htm) which is against [IBM's terms of service](http://www.ibm.com/legal/us/en/). In other words: copying the text is a copyright infringement. Please do not copy text from other sources like that. Instead, please link to the page instead. So please edit your answer and remove the copied text. Thanks. – DarkDust Jan 12 '12 at 17:39
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.

JeremyP
  • 84,577
  • 15
  • 123
  • 161
2

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

Emre Gürses
  • 1,992
  • 1
  • 23
  • 28
1

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.

Community
  • 1
  • 1
Jeremy
  • 8,902
  • 2
  • 36
  • 44
1

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.

AKS
  • 31
  • 2