title: Printing the binary form of bytes in java, "==" not working?
body: <p>Firstly I know you can use methods to print the binary representation in java, I just wanted to do it myself for fun. </p>
  
  <p>I've managed to do it for integers just fine with the code below.</p>
  
  <pre><code>public static void printBinary(int n) {
  
      int mask = n;
      mask = mask ^ ~mask;    //make sure mask is 1111...
      mask = mask &lt;&lt; 31;
  
      for (int i = 0; i &lt; 32; i++) {
  
          int test = mask &amp; n;
  
          if (test == mask) {
  
              System.out.print("1");
          }
          else {
              System.out.print("0");
          }
  
          if ((i + 1) % 4 == 0) {
              System.out.print(" ");
          }
  
          mask = mask &gt;&gt;&gt; 1;
      }
  
  }
  </code></pre>
  
  <p>I thought bytes wouldn't be much different so I made this method for bytes:</p>
  
  <pre><code>public static void printBinary(byte n) {
      byte mask = n;
      mask = (byte) (mask ^ ~mask);
      mask = (byte) (mask &lt;&lt; 7);
  
  
      for (int i = 0; i &lt; 8; i++) {
  
          byte test = (byte) (mask &amp; n);
  
          if (test == mask) {
              System.out.print("1");
          }
          else {
              System.out.print("0");
          }
  
          if ((i + 1) % 4 == 0) {
              System.out.print(" ");
          }
  
          mask = (byte) (mask &gt;&gt;&gt; 1);
      }
  }
  </code></pre>
  
  <p>The result I get for this, no matter what value of of n is just 0000 0000. How come the boolean operator == doesn't seem to be working as intended? Or perhaps that isn't the problem as I suspect.</p>
  
  <p>Any ideas?</p>