title: how to do bit shifts and masks in haskell?
body: <p>I'm writing a routine to determine whether the high 16 bits of a 32-bit integer have more bits set, or the low bits.</p>
  
  <p>In C, I would write this:</p>
  
  <pre><code>bool more_high_bits(int a) {
    if ((a &gt;&gt; 16) == 0) return false;    // no high bits
    if ((a &amp; 0xFFFF) == 0) return true;  // no low bits
  
    // clear one high bit and one low bit, and ask again
    return more_high_bits(a&amp;(a - 0x10001));
  }
  </code></pre>
  
  <p>So in Haskell, I'm trying this:</p>
  
  <pre><code>more_high_bits a=if (a `shiftR` 16) /= 0 then 0 else
                   if ((.&amp;.) a 65535) /= 0 then 1 else
                   more_high_bits((.&amp;.) a (a-65537))
  </code></pre>
  
  <p>But it just times out.</p>
  
  <p>What am I doing wrong?  What's the more idiomatic way to do this?  Please don't code away the shift or the &amp; because I'd like to know how I "should" be using these.</p>
  
  <p>Addendum: I tried this code out on an haskell compiler:</p>
  
  <p><a href="http://www.tutorialspoint.com/compile_haskell_online.php" rel="noreferrer">http://www.tutorialspoint.com/compile_haskell_online.php</a></p>
  
  <pre><code>import Data.Bits
  
  g a=if (a `shiftR` 16) == 0 then 0 else
      if ((.&amp;.) a 65535) == 0 then 1 else
      g((.&amp;.) a (a-65537))
  
  main = print (g(237))
  </code></pre>
  
  <p>But it tells me "No instance for (Bits a0) arising from a use of 'g'
  The type variable 'a0' is ambiguous"</p>
  
  <p>What is "a0"??</p>