Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<< operator in objective c enum?

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

like image 762
shannoga Avatar asked Dec 28 '11 16:12

shannoga


2 Answers

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.

like image 157
joerick Avatar answered Sep 21 '22 19:09

joerick


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

like image 38
Ali3n Avatar answered Sep 20 '22 19:09

Ali3n