zxing/zxing · error · IllegalArgumentException

Sizes don't match

Error message

Sizes don't match

What it means

Thrown by BitArray.xor(BitArray other) when this.size != other.size. The XOR operation works by iterating over the raw int[] backing arrays element-by-element, so both arrays must represent exactly the same number of bits to avoid corrupting trailing padding bits or reading out of bounds.

Source

Thrown at core/src/main/java/com/google/zxing/common/BitArray.java:260

      if ((value & (1 << numBitsLeft)) != 0) {
        bits[nextSize / 32] |= 1 << (nextSize & 0x1F);
      }
      nextSize++;
    }
    size = nextSize;
  }

  public void appendBitArray(BitArray other) {
    int otherSize = other.size;
    ensureCapacity(size + otherSize);
    for (int i = 0; i < otherSize; i++) {
      appendBit(other.get(i));
    }
  }

  public void xor(BitArray other) {
    if (size != other.size) {
      throw new IllegalArgumentException("Sizes don't match");
    }
    for (int i = 0; i < bits.length; i++) {
      // The last int could be incomplete (i.e. not have 32 bits in
      // it) but there is no problem since 0 XOR 0 == 0.
      bits[i] ^= other.bits[i];
    }
  }

  /**
   *
   * @param bitOffset first bit to start writing
   * @param array array to write into. Bytes are written most-significant byte first. This is the opposite
   *  of the internal representation, which is exposed by {@link #getBitArray()}
   * @param offset position in array to start writing
   * @param numBytes how many bytes to write
   */
  public void toBytes(int bitOffset, byte[] array, int offset, int numBytes) {
    for (int i = 0; i < numBytes; i++) {

View on GitHub (pinned to 19aa2d8254)

Solutions

  1. Verify both BitArrays have the same getSize() before calling xor(), and log the mismatch if they differ
  2. Ensure both arrays are constructed from the same dimension parameter — in QR encoding, both the data matrix and mask are created from the same version's module count
  3. If sizes legitimately differ, zero-pad the shorter array to match before XORing

Example fix

// before
data.xor(mask); // mask was built for a different version

// after
if (data.getSize() != mask.getSize()) {
    throw new IllegalStateException(
        "Mask size " + mask.getSize()
        + " != data size " + data.getSize());
}
data.xor(mask);
Defensive patterns

Strategy: validation

Validate before calling

static void xorSafe(BitArray a, BitArray b) {
    if (a.getSize() != b.getSize()) {
        throw new IllegalStateException(
            "Cannot XOR: size " + a.getSize()
            + " vs " + b.getSize());
    }
    a.xor(b);
}

Type guard

static boolean sameSize(BitArray a, BitArray b) {
    return a.getSize() == b.getSize();
}

Prevention

When it happens

Trigger: Calling xor() on two BitArrays that were built independently with different lengths — e.g., XORing a data BitArray with a mask pattern BitArray where the mask was generated for a different QR code version.

Common situations: QR code mask application where the mask matrix was built with the wrong dimension; unit tests that create mismatched BitArrays; refactoring that changes how one of the arrays is sized.

Related errors


AI-assisted analysis of zxing/zxing@19aa2d8254 (2026-08-14). Data as JSON: /api/errors/50fe0a85d742856c. Report an issue: GitHub.