Presentation is loading. Please wait.

Presentation is loading. Please wait.

Podcast Ch23a Title: Bit Arrays

Similar presentations


Presentation on theme: "Podcast Ch23a Title: Bit Arrays"— Presentation transcript:

1 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

2 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.

3 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. Bit Operations

4 Bit Arrays (continued)

5 What is the output? byte x = 23, y = 13, z; z = (byte)(x | y);
System.out.println(Integer.toString(z,2)); // z = (byte)(x & y); // 101 z = (byte)(x ^ y); // z = (byte)(~0 & y); // 1101

6 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 = x << 2 = x = x >> 3 = x = x >>> 3 =

7 What is the output? byte x = 23, y = 13, z; z = (byte)(x << 2);
System.out.println(Integer.toString(z,2)); // z = (byte)(x >> 2); // 101 z = (byte)(x >>> 3); // 10 z = (byte)(~0 << 3 | y); // -11

8 Bit Arrays (continued)

9 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) { }

10 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) { } . . . }

11 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)

12 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)

13 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()

14 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 = x.clear(2); // x = z = x.or(y); // z = x.and(y); // z = x.xor(y); // z = x.not(); // z = x.shiftLeft(2); // z = x.shiftSignedRight(2); // z = x.shiftUnsignedRight(2); // z.assignInt(31); System.out.println(z);// z.assignChar('a'); System.out.println(z);//

15 What is the output? int[] arr = {1, 0, 1, 1, 0, 1, 1, 0};
BitArray x = new BitArray(1), y = new BitArray(arr); x.assignByte((byte)28); System.out.println(x.or(y)); // System.out.println(x.and(y)); // System.out.println(x.xor(y)); //

16 What is the output? int[] arr = {1, 0, 1, 1, 0, 1, 1, 0};
BitArray x, y = new BitArray(arr), mask = new BitArray(8); mask = mask.not(); mask = mask.shiftUnsignedRight(1); x = y.and(mask); System.out.println(x); //


Download ppt "Podcast Ch23a Title: Bit Arrays"

Similar presentations


Ads by Google