public BitVector(int nbits) { this(); // throw an exception to be consistent // but (do we want to be consistent?) if (nbits < 0) { throw new NegativeArraySizeException("nbits < 0: " + nbits); } // even though the array's length is loosely kept to that of Sun's // "logical // length," this might help in some cases where code uses size() to fill // in // bits after constructing a BitSet, or after having one passed in as a // parameter. setLengthWords(array, wordIndex(nbits + 31)); }
public void and(BitVector set) { // a & a is just a if (this == set) { return; } // trim the second set to avoid extra work trimToSize(set.array); // check if the length is longer than otherLength int otherLength = set.array.length(); if (array.length() > otherLength) { // shrink the array, effectively ANDing those bits to false setLengthWords(array, otherLength); } // truth table // // case | a | b | a & b | change? // 1 | false | false | false | a is already false // 2 | false | true | false | a is already false // 3 | true | false | false | set a to false // 4 | true | true | true | a is already true // // we only need to change something in case 3, so iterate over set a int index = 0; while ((index = nextSetWord(array, index)) != -1) { setWord(array, index, array.get(index) & getWord(set.array, index)); index++; } }
setLengthWords(array, newLength);