title: The use of bitmasks in Objective-C: how iOS handles options stored in bitmasks?
body: <p>So, I started learning iOS development and Objective-C recently, mainly using Stanford's free course on iTunes U.</p>
  
  <p>I ended up stumbling across bitmasks, something widely used in iOS APIs but that I'm not very familiar with. I've read some stuff about it and now I understand its basics, at least.</p>
  
  <p>In this Stanford course we are developing a card matching game, like the game Concentration. Cards are represented by buttons. The normal state (<code>UIControlStateNormal</code>) represents the back of the card, while the selected state (<code>UIControlStateSelected</code>) represents its front (that is, its contents, a string property called <code>card.contents</code>, like "A♣"). If two cards match, they become unplayable, so they get the disabled state (<code>UIControlStateDisabled</code>) while already in the selected state. <a href="http://i.imgur.com/il1tKMc.jpg" rel="nofollow"><em>See this image for reference.</em></a></p>
  
  <p>In the <a href="https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIControl_Class/Reference/Reference.html#//apple_ref/doc/constant_group/Control_State" rel="nofollow">Apple documentation about Control States</a>, we can find this bitmask defining the possible states:</p>
  
  <pre><code>enum {
     UIControlStateNormal               = 0,
     UIControlStateHighlighted          = 1 &lt;&lt; 0,
     UIControlStateDisabled             = 1 &lt;&lt; 1,
     UIControlStateSelected             = 1 &lt;&lt; 2,
     UIControlStateApplication          = 0x00FF0000,
     UIControlStateReserved             = 0xFF000000
  };
  </code></pre>
  
  <p>At some point, in the code done by the Stanford professor in-lecture, the following code is used to set <code>card.contents</code> as the title for both the selected state (the front of the card) and the selected and disabled states combined (a matched card):</p>
  
  <pre><code>[cardButton setTitle:card.contents forState:UIControlStateSelected];
  [cardButton setTitle:card.contents
              forState:UIControlStateSelected|UIControlStateDisabled];
  </code></pre>
  
  <p>What I don't understand is, why do we need the first line? I thought the second one would be enough, since it's setting the title for the button by combining two states using an OR, so I interpreted that it already "covers" the case when the card is only in the selected state.</p>
  
  <p>After some tests, I was clearly wrong, so I don't quite understand how iOS handles options stored in bitmasks. Can you help me?</p>
  
  <p>Just one more thing: in the enum declaration above, the first four constants are defined as <code>0</code>, <code>1 &lt;&lt; 0</code>, <code>1 &lt;&lt; 1</code>, and <code>1 &lt;&lt; 2</code>, that is, <code>0</code>, <code>1</code>, <code>2</code>, and <code>4</code>. Why the developer defined the fifth and the sixth as <code>0x00FF0000</code> and <code>0xFF000000</code>, and not <code>1 &lt;&lt; 3</code> and <code>1 &lt;&lt; 4</code>?</p>
  
  <p>Thanks in advance!</p>