10

everyone. When learning OC recently, I always come across enum like this.

enum {
    type1  = 0,
    type2  = 1 << 0,
    type3  = 1 << 1,
};

What does it mean by type = 1 << 0 ? What is it usually used for? Thanks in forward.

tia
  • 9,518
  • 1
  • 30
  • 44
Calios
  • 806
  • 9
  • 23
  • 3
    It is left-shift bitwise operator. `1 << 0` is practically equal to 1 but the enum author just want to make it clearer that it is in the same group of latter enums `type3` `type4` etc. – tia Jun 07 '13 at 08:02
  • 1
    Really strange and unusual to include 0 though, since you can't treat `type1` as one of the others. – unwind Jun 07 '13 at 08:04
  • 2
    Assigning values to enum like that is actually a good way to represent flags. For example, you can check the value of the enum variable and determine if it is both type2 and type3 by bitwise ANDing it with (type2 | type3), `(var & (type2 | type3)) == (type2 | type3)` means it is both type2 and type3. Just saying. :) – n3rd4n1 Jun 07 '13 at 08:15
  • Got another tip. So happy! @n3rd4n1 – Calios Jun 07 '13 at 08:18
  • Read also: [What is the meaning of this declaration?](http://stackoverflow.com/questions/15708493/what-is-the-meaning-of-this-declaration/15708566#15708566) – Grijesh Chauhan Jun 07 '13 at 08:40
  • @GrijeshChauhan So vivid the figure is! Happy! – Calios Jun 07 '13 at 08:43
  • @Lilac thanks! I drawn using [ASCIIFLOW](http://www.asciiflow.com/#Draw) – Grijesh Chauhan Jun 07 '13 at 08:45
  • @GrijeshChauhan Poor internet speed that I'm still unable to open the link. I'm sure I'll collect it and try sometime later. It seems so fun. – Calios Jun 07 '13 at 09:01
  • @Lilac :) :) yes sure its good tool. I use it for my Q&A – Grijesh Chauhan Jun 07 '13 at 09:03
  • @GrijeshChauhan Another little question: what if I wanna add more types in the enum? type4 = 2 << 1 ? Then it's 4, missing 3. type4 = 3 <<0? Then what's next? A little confusing. – Calios Jun 07 '13 at 09:26
  • @Lilac Sorry couldn't understand you Q clearly, but yes you can add two `enum` constants, enums are `int` actually. – Grijesh Chauhan Jun 07 '13 at 19:08
  • @GrijeshChauhan I mean if I want to have more than three types in enum, like five, what should I add depending on my original enum without destroying the uniformity? type 4 = ? type5 = ? – Calios Jun 08 '13 at 01:25
  • @GrijeshChauhan God helps me! I happened to see the following codes in UIControl.h. typedef NS_OPTIONS(NSUInteger, UIControlEvents) { UIControlEventTouchDown = 1 << 0, UIControlEventTouchDownRepeat = 1 << 1, UIControlEventTouchDragInside = 1 << 2, UIControlEventTouchDragOutside = 1 << 3, UIControlEventTouchDragEnter = 1 << 4, UIControlEventTouchDragExit = 1 << 5, UIControlEventTouchUpInside = 1 << 6, UIControlEventTouchUpOutside = 1 << 7, UIControlEventTouchCancel = 1 << 8... }; – Calios Jun 08 '13 at 03:24
  • @Lilac yes you got correct!, you can add in same enum definition – Grijesh Chauhan Jun 08 '13 at 03:42

5 Answers5

8

Bitwise Shift Left Operator

In Objective-C the bitwise left shift operator is represented by the '<<' sequence, followed by the number of bit positions to be shifted

Source

Also read this famous post to understand what it does and how

Community
  • 1
  • 1
Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
  • 1
    That's really a fantastic post! I just want a drop of water, but you send me a whole spring. I'll read over it and thanks again. :) – Calios Jun 07 '13 at 08:14
7

<< is binary operator

1 << 0 = 1
1 << 1 = 2
1 << 2 = 4
SiMet
  • 614
  • 7
  • 18
5

It is one bit shift. Such a construction could be used if you need to assign several types to something. It's called bit mask. Example:

enum {
    type1  = 1,
    type2  = 1 << 1,
    type3  = 1 << 2,
};

Means that type1 is binary 00000001, type2 is 00000010, type3 is 00000100 and so on. So, if type mask is 3 (00000011) you know that your object is type1 and type2.

Stas
  • 641
  • 8
  • 16
4

This operator is a bitwise shift (not only in objective-c).

you can assign every entry in an enum an integervalue, so it is the same as

enum {
    type1  = 0,
    type2  = 1,
    type3  = 2
};

you can use the shift-operator to easily assure that your enum entries can be bitwise added like

int bitmask = type2 | type3 //bitmask = 3
Herm
  • 2,956
  • 19
  • 32
0

This offset by zero bits, i.e. his absence. Just for uniformity.

AlGol
  • 1