Download presentation
Presentation is loading. Please wait.
Published byHendra Sugiarto Cahyadi Modified over 5 years ago
1
Podcast Ch23b Title: BitArray Implementation
Description: Overview; constructors; operators; bit 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
2
BitArray Class Implementation
The BitArray class stores the bits in a byte array. Methods map bit numbers into the correct bit in the array.
3
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; }
4
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.
5
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))); }
6
BitArray Class Implementation (continued)
7
What is the output of the following statements?
int[] xarr = {1, 1, 0, 1, 0, 1, 0, 1}, yarr = {0, 1, 1, 1, 0, 0, 1, 1}; BitArray x = new BitArray(xarr), y = new BitArray(yarr); y.clear(2); System.out.println("clear bit 2 in y = " + y); // clear bit 2 in y = y.set(5); System.out.println("set bit 5 in y = " + y); // set bit 5 in y = System.out.println("shift x left 3 = " + x.shiftLeft(3)); // shift x left 3 =
8
BitArray Constructors
There are two constructors that create a BitArray object. One creates an empty bit array of a specified size; the second constructor initializes the bit array by using a Java integer array of 0 and 1 values.
9
Constructor (creates empty bit array)
// constructor; create bit array of numBits bits // each having value 0 public BitArray(int numBits) { numberOfBits = numBits; // number of bytes 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; }
10
BitArray Operators Implement BitArray operator by creating an object tmp and initialize its byte array by doing the bitwise operation on the operands. Return the object as the value of the operator.
11
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");
12
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; }
13
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; }
14
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); }
15
What BitArray method do the following statements implement?
public void ??(int i) { if (i < 0 || i >= numberOfBits) throw new IndexOutOfBoundsException( "bit out of range"); member[arrayIndex(i)] |= bitMask(i); } Answer: set
16
What is the output of the following statements?
int[] xarr = {1, 1, 0, 1, 0, 1, 0, 1}, yarr = {0, 1, 1, 1, 0, 0, 1, 1}; BitArray x = new BitArray(xarr), y = new BitArray(yarr); System.out.println("x = " + x); // x = System.out.println("y = " + y); // y = System.out.println("x & y = " + x.and(y)); // x & y = System.out.println("x | y = " + x.or(y)); // x | y = System.out.println("~x = " + x.not()); // ~x = NOTE: not used in podcast
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.