<<

Podcast Ch23b • Title: BitArray Implementation • Description: Overview; constructors; operators; access and modification • Participants: Barry Kurtz (instructor); John Helfert and Tobie Williams (students) • Textbook: Data Structures for Java; William H. Ford and William R. Topp BitArray Class Implementation

• The BitArray class stores the in a array. Methods map bit numbers into the correct bit in the array. BitArray Class Implementation (continued) • The private method arrayIndex() determines the array element to which bit i belongs.

// determine the index of the array element // containing bit i private int arrayIndex(int i) { return i/8; } BitArray Class Implementation (continued) • After locating the correct array index, apply the method bitMask() that returns a byte value containing a 1 in the bit position representing i. This value, called a mask, can be used to set or clear the bit. BitArray Class Implementation (continued)

// bit i is represented by a bit in // member[arrayIndex(i)]; return a byte // value with a 1 in the position that // represents bit i private byte bitMask(int i) { // use & to find the remainder after // dividing by 8; remainder 0 puts a // 1 in the left-most bit and 7 puts // a 1 in the right-most bit return (byte)(1 << (7 - (i & 7))); } BitArray Class Implementation (continued) BitArray Constructors

• There are two constructors that create a BitArray object. One creates an empty of a specified size; the second constructor initializes the bit array by using a Java integer array of 0 and 1 values. Constructor (creates empty bit array) // constructor; create bit array of numBits bits // each having value 0 public BitArray(int numBits) { numberOfBits = numBits;

// number of needed to hold // numberOfBits elements byteArraySize = (numberOfBits+7)/8;

// initialize the array with all bytes 0 member = new byte[byteArraySize]; for (int i=0; i < member.length; i++) member[i] = 0; } BitArray Operators • Implement BitArray operator by creating an object tmp and initialize its byte array by doing the on the operands. Return the object as the value of the operator. BitArray Operator or()

// bitwise OR public BitArray or(BitArray x) { int i;

// the bit arrays must have the same size if (numberOfBits != x.numberOfBits) throw new IllegalArgumentException( "BitArray |: bit arrays are " + "not the same size"); BitArray Operator or() (concluded)

// form the bitwise OR in tmp BitArray tmp = new BitArray(numberOfBits);

// each member element of tmp is the bitwise // OR of the current object and x for (i = 0; i < byteArraySize; i++) tmp.member[i] = (byte)(member[i] | x.member[i]);

// return the bitwise OR return tmp; } Bit Access and Modification Methods • Use arrayIndex() and bitMask() to access and modify an individual bit in a BitVector. // return value of bit i public int bit(int i) { // is i in range 0 to numberOfBits-1 ? if (i < 0 || i >= numberOfBits) throw new IndexOutOfBoundsException( "BitArray bit(): bit out of range");

// return the bit corresponding to i if ((member[arrayIndex(i)] & bitMask(i)) != 0) return 1; else return 0; } Bit Access and Modification Methods (concluded)

// clear bit i public void clear(int i) { // is i in range 0 to numberOfBits-1 ? if (i < 0 || i >= numberOfBits) throw new IndexOutOfBoundsException( "BitArray clear(): bit out of range");

// clear the bit corresponding to i; note // that ~bitMask(i) has a 0 in the bit; // we are interested in a 1 in all others member[arrayIndex(i)] &= ~bitMask(i); }