class ByteArrayHelper {

  public static void setBit(byte[] input, int index, boolean choice) {
    if (index < 0) {
      throw new IllegalAccessError("Bit index must not be negative.");
    }
    if (choice) {
      // We read bits from left to right, hence the 7 - x.
      // Put a 1 in the correct position of a
      // zero-byte and OR it into the correct byte to ensure that the position
      // becomes 1 no matter whether it is currently set or not.
      input[index / 8] |= ((byte) 0x01) << (7 - (index % 8));
    } else {
      // Construct an all 1-byte, then construct a byte like above, where only
      // the correct position is set to 1. We XOR these bytes to get a byte
      // which is all 1's except in the correct position. We AND this into the
      // correct byte to ensure that only the correct positions gets set to 0.
      input[index / 8] &= 0xFF ^ ((byte) 0x01) << (7 - (index % 8));
    }
  }

  private  ByteArrayHelper();

}