LongRangeSet (LongRangeSet other) { if (this.isEmpty() || other.isEmpty()) return empty(); long[] left = splitAtZero(asRangeArray()); long[] right = splitAtZero(other.asRangeArray()); // More than three intervals --> convert to single interval to make result more compact (though probably less precise) if (left.length > 6) { left = splitAtZero(new long[]{left[0], left[left.length - 1]}); } if (right.length > 6) { right = splitAtZero(new long[]{right[0], right[right.length - 1]}); } BitString globalMask = getBitwiseMask().and(other.getBitwiseMask()); globalMask = new BitString(globalMask.myBits | ~globalMask.myMask, -1L); LongRangeSet result = empty(); for (int i = 0; i < left.length; i += 2) { for (int j = 0; j < right.length; j += 2) { result = result.join(bitwiseAnd(left[i], left[i + 1], right[j], right[j + 1], globalMask)); } } return result; }