title: Java boolean[] to byte[] and back
body: <p>I am sending <code>byte[]</code> arrays over a socket connection in Java.</p>
  
  <p>I have a pretty long <code>boolean[]</code> array, where <code>array.length % 8 == 0</code>.</p>
  
  <p>I'd like to convert this <code>boolean[]</code> array into a <code>byte[]</code> array with 8 times as few elements, so that I can then send the <code>byte[]</code> over the socket connection.</p>
  
  <p>The <code>boolean[]</code> array looks like this: <code>01011010 10101010 01100011 11001010</code> etc.</p>
  
  <p>The <code>byte[]</code> in this case should look like this: <code>0x5A 0xAA 0x63 0xCA</code>.</p>
  
  <p>I have found some code on another question on how to convert a single <code>byte</code> into a <code>boolean[]</code> array and added a new method to it to convert an entire array here:</p>
  
  <pre><code>public static boolean[] booleanArrayFromByteArray(byte[] x) {
      boolean[] y = new boolean[x.length * 8];
      int position = 0;
      for(byte z : x) {
          boolean[] temp = booleanArrayFromByte(z);
          System.arraycopy(temp, 0, y, position, 8);
          position += 8;
      }
      return y;
  }
  
  public static boolean[] booleanArrayFromByte(byte x) {
      boolean bs[] = new boolean[4];
      bs[0] = ((x &amp; 0x01) != 0);
      bs[1] = ((x &amp; 0x02) != 0);
      bs[2] = ((x &amp; 0x04) != 0);
      bs[3] = ((x &amp; 0x08) != 0);
      return bs;
  }
  </code></pre>
  
  <p>I'd like to know if there is a more efficient way of doing this.</p>
  
  <p>Edit: Thanks</p>