From 9ba62e25b910b7288ab08d497032bd649b8eb474 Mon Sep 17 00:00:00 2001 From: Zsombor Date: Mon, 16 Feb 2009 17:51:37 -0700 Subject: [PATCH] fix and-ing, or-ing, etc BitSet with variant size --- classpath/java/util/BitSet.java | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/classpath/java/util/BitSet.java b/classpath/java/util/BitSet.java index eff0cfafdf..3b1e80bc61 100644 --- a/classpath/java/util/BitSet.java +++ b/classpath/java/util/BitSet.java @@ -45,16 +45,20 @@ public class BitSet implements Serializable, Cloneable { } public void and(BitSet otherBits) { - int max = Math.max(bits.length, otherBits.bits.length); - for (int i = 0; i < max; i++) { + int min = Math.min(bits.length, otherBits.bits.length); + for (int i = 0; i < min; i++) { bits[i] &= otherBits.bits[i]; } + for (int i = min; i < bits.length; i++) { + bits[i] = 0; + } } public void andNot(BitSet otherBits) { int max = Math.max(bits.length, otherBits.bits.length); enlarge(max); - for (int i = 0; i < max; i++) { + int min = Math.min(bits.length, otherBits.bits.length); + for (int i = 0; i < min; i++) { bits[i] &= ~otherBits.bits[i]; } } @@ -62,7 +66,8 @@ public class BitSet implements Serializable, Cloneable { public void or(BitSet otherBits) { int max = Math.max(bits.length, otherBits.bits.length); enlarge(max); - for (int i = 0; i < max; i++) { + int min = Math.min(bits.length, otherBits.bits.length); + for (int i = 0; i < min; i++) { bits[i] |= otherBits.bits[i]; } } @@ -70,13 +75,14 @@ public class BitSet implements Serializable, Cloneable { public void xor(BitSet otherBits) { int max = Math.max(bits.length, otherBits.bits.length); enlarge(max); - for (int i = 0; i < max; i++) { + int min = Math.min(bits.length, otherBits.bits.length); + for (int i = 0; i < min; i++) { bits[i] ^= otherBits.bits[i]; } } private void enlarge(int newSize) { - if (bits == null || bits.length <= newSize) { + if (bits == null || bits.length < newSize) { long[] newBits = new long[newSize + 1]; if (bits != null) { System.arraycopy(bits, 0, newBits, 0, bits.length);