<< operator in objective c enum?

后端 未结 6 1192
别跟我提以往
别跟我提以往 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: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

提交回复
热议问题