Podcast Ch23a
Total Page:16
File Type:pdf, Size:1020Kb
Podcast Ch23a • Title: Bit Arrays • Description: Overview; bit operations in Java; BitArray class • Participants: Barry Kurtz (instructor); John Helfert and Tobie Williams (students) • Textbook: Data Structures for Java; William H. Ford and William R. Topp Bit Arrays • Applications such as compiler code generation and compression algorithms create data that includes specific sequences of bits. – Many applications, such as compilers, generate specific sequences of bits. Bit Arrays (continued) • Java binary bit handling operators |, &, and ^ act on pairs of bits and return the new value. The unary operator ~ inverts the bits of its operand. BitBit OperationsOperations x y ~x x | y x & y x ^ y 0 0 1 0 0 0 0 1 1 1 0 1 1 0 0 1 0 1 1 1 0 1 1 0 Bit Arrays (continued) Bit Arrays (continued) • Operator << shifts integer or char values to the left. Operators >> and >>> shift values to the right using signed or unsigned arithmetic, respectively. Assume x and y are 32-bit integers. x = 0...10110110 x << 2 = 0...1011011000 x = 101...11011100 x >> 3 = 111101...11011 x = 101...11011100 x >>> 3 = 000101...11011 Bit Arrays (continued) Before performing the bitwise operator |, &, or ^, Java performs binary numeric promotion on the operands. The type of the bitwise operator expression is the promoted type of the operands. The rules of promotion are as follows: • If either operand is of type long, the other is converted to long. • Otherwise, both operands are converted to type int. In the case of the unary operator ~, Java converts a byte, char, short to int before applying the operator, and the resulting value is an int. BitArray Class • BitArray class lets programmers use bit operations at a higher level than the "down and dirty" use of the Java bit operators. public class BitArray { // number of bits in the bit array private int numberOfBits; // number of byte values used for the bit array private int byteArraySize; // the array itself private byte[] member; // constructor; create bit array of numBits // bits having value 0 public BitArray(int numBits) { . } BitArray Class (continued) // constructor; let n = b.length; creates a // bit array whose bits are initialized // as follows: // bit 0: b[0] // bit 1: b[1] // ... // bit n-1: b[n-1] public BitArray(int[] b) { . } . } BitArray Class (continued) • BitArray class methods: – Conversion from primitive type: public void assignChar(char c) public void assignInt(int n) – Bit access and update: public int bit(int i) public void set(int i) BitArray Class (continued) – Bit operators: public BitArray and(BitArray x) public BitArray or(BitArray x) public BitArray xor(BitArray x) public BitArray not() public BitArray shiftLeft(int n) public BitArray shiftRight(int n) public BitArray shiftUnsigned(int n) BitArray Class (continued) – Input/Output: public void read(DataInputStream istr, int numBits) public void write(DataOutputStream ostr) public String toString() – Miscellaneous: public void clear() public void clear(int i) public boolean equals (Object x) public int size() BitArray Class (continued) int[] a = {1, 0, 1, 1, 0, 0}, b = {1, 0, 0, 0, 1, 0}; BitArray x = new BitArray(a), y = new BitArray(b), z = new BitArray(a.length); y.set(0); y.set(4); // y = 100010 x.clear(2); // x = 100100 z = x.or(y); // 100110 z = x.and(y); // 100000 z = x.xor(y); // 000110 z = x.not(); // 011011 z = x.shiftLeft(2); // 010000 z = x.shiftSignedRight(2); // 111001 z = x.shiftUnsignedRight(2); // 001001 z.assignInt(31); System.out.println(z);//00000000000000000000000000011111 z.assignChar('a'); System.out.println(z);// 0000000001100001.