title: Create a mask that marks the most significant set bit, using only bitwise operators
body: <p>This was part of a larger programming assignment that was due for me last night. Couldn't figure out this problem, but I'm curious as to how it could be solved.</p>
  
  <p>The function <code>int greatestBitPos(int x)</code> should return an int mask that marks the position of the most significant bit. If x==0, return 0. No control structures (if, while, ?:) allowed.</p>
  
  <p>Example: <code>greatestBitPos(96) = 0x40</code></p>
  
  <p>Legal operators: ! ~ &amp; ^ | + &lt;&lt; >> =</p>
  
  <p><a href="http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog" rel="noreferrer">This website on bit twiddling</a> is something I used as a starting point, especially the second algorithm. However, it uses <code>&lt;</code> comparisons, something this problem doesn't allow.</p>
  
  <p>All ideas are welcome, thanks!</p>
  
  <p>Edit: Please assume 2's complement, 32-bit integers. For all negative numbers, they have their topmost bit set, so the return value should be <code>0x80000000</code>.</p>