title: Bit shifting and bit mask - sample code
body: <p>I've come across some code which has the bit masks <code>0xff</code> and <code>0xff00</code> or in 16 bit binary form <code>00000000 11111111</code> and <code>11111111 00000000</code>.</p>
  
  <pre><code>/**
   * Function to check if the given string is in GZIP Format.
   *
   * @param inString String to check.
   * @return True if GZIP Compressed otherwise false.
   */
  public static boolean isStringCompressed(String inString)
  {
      try
      {
          byte[] bytes = inString.getBytes("ISO-8859-1");
          int gzipHeader = ((int) bytes[0] &amp; 0xff)
              | ((bytes[1] &lt;&lt; 8) &amp; 0xff00);
          return GZIPInputStream.GZIP_MAGIC == gzipHeader;
      } catch (Exception e)
      {
          return false;
      }
  }
  </code></pre>
  
  <p>I'm trying to work out what the purpose of using these bit masks in this context (against a byte array). I can't see what difference it would make?</p>
  
  <p>In the context of a GZip compressed string as this method seems to be written for the GZip magic number is <code>35615</code>, <code>8B1F</code> in Hex and <code>10001011 00011111</code> in binary.</p>
  
  <p>Am I correct in thinking this swaps the bytes? So for example say my input string were <code>\u001f\u008b</code></p>
  
  <pre><code>bytes[0] &amp; 0xff00
   bytes[0] = 1f = 00011111
            &amp; ff = 11111111
                   --------
                 = 00011111
  
  bytes[1] &lt;&lt; 8
   bytes[1] = 8b = 10001011
            &lt;&lt; 8 = 10001011 00000000
  
  ((bytes[1] &lt;&lt; 8) &amp; 0xff00)
  = 10001011 00000000 &amp; 0xff00
  = 10001011 00000000 
    11111111 00000000 &amp;
  -------------------
    10001011 00000000
  </code></pre>
  
  <p>So</p>
  
  <pre><code>00000000 00011111
  10001011 00000000 |
  -----------------
  10001011 00011111 = 8B1F
  </code></pre>
  
  <p>To me it doesn't seem like the <code>&amp;</code> is doing anything to the original byte in both cases <code>bytes[0] &amp; 0xff</code> and <code>(bytes[1] &lt;&lt; 8) &amp; 0xff00)</code>. What am I missing?</p>