Podcast Ch23b Title: BitArray Implementation

Slides:



Advertisements
Similar presentations
Queues1 Part-B2 Queues. Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme.
Advertisements

Arrays. Memory organization Table at right shows 16 bytes, each consisting of 8 bits Each byte has an address, shown in the column to the left
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
1 Dynamic Arrays  Why Dynamic Arrays?  A Dynamic Array Implementation  The Vector Class  Program Example  Array Versus Vector.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 23 Bit Arrays.
Object-Oriented Programming (OOP). Implementing an OOD in Java Each class is stored in a separate file. All files must be stored in the same package.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Bitwise Operators Fall 2008 Dr. David A. Gaitros
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 13 Implementing.
Lab 1 Logbook ADT. OVERVIEW A monthly logbook consists of a set of entries, one for each day of the month.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
Review. Problem 1 What is the output of the following piece of code void increment(int x) { x++; } int main() { int y = 10; increment(y); cout
The ArrayList Data Structure The Most Important Things to Review.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 20 Ordered.
 Array ◦ Single & Multi-dimensional  Java Operators ◦ Assignment ◦ Arithmetic ◦ Relational ◦ Logical ◦ Bitwise & other.
August 6, Operators. August 6, Arithmetic Operators.
ME2008– W05 MID1- Reference 2016Q1- Source: Deitel /C- How To.
Queues By Jimmy M. Lu. Overview Definition Standard Java Queue Operations Implementation Queue at Work References.
Podcast Ch24c Title: Breadth First Search
Bitwise Operations C includes operators that permit working with the bit-level representation of a value. You can: - shift the bits of a value to the left.
Podcast Ch23e Title: Implementing Huffman Compression
Podcast Ch17b Title: Iterative Tree Traversal
EECE 310: Software Engineering
Two-Dimensional Arrays
Podcast Ch24d Title: Depth First Search and Acyclic Graphs
Podcast Ch26a Title: Representing Graphs
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
The Class ArrayLinearList
Stacks.
Podcast Ch17d Title: Drawing a Binary Tree
Bits and Bytes Hex Digit Bit Pattern
Podcast Ch17a Title: Expression Trees
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Priority Queues.
Programming in Java Lecture 11: ArrayList
Operators August 6, 2009.
Bitwise Operators CS163 Fall 2018.
C Operators, Operands, Expressions & Statements
Queues 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Podcast Ch25d Title: Minimum Path Algorithm
Podcast Ch18b Title: STree Class
Podcast Ch24b Title: Graphs and Digraphs
COMPUTER 2430 Object Oriented Programming and Data Structures I
Podcast Ch22c Title: Deleting from a Heap
Podcast Ch18c Title: BST delete operation
Podcast Ch25c Title: Shortest Path Algorithm
Podcast Ch23f Title: Serialization
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
Podcast Ch22b Title: Inserting into a Heap
Podcast Ch18a Title: Overview of Binary Search Trees
Podcast Ch20b Title: TreeMap Design
Podcast Ch18d Title: Binary Search Tree Iterator
Podcast Ch21d Title: Hash Class Iterators
Podcast Ch26b Title: Digraph Class Implementation
MIPS assembly.
Podcast Ch27a Title: Overview of AVL Trees
Podcast Ch21a Title: Hash Functions
Podcast Ch21f Title: HashSet Class
Podcast Ch23d Title: Huffman Compression
Podcast Ch27b Title: AVLTree implementation
Podcast Ch22a Title: Array-based Binary Trees
Podcast Ch24b Title: Strongly Connected Components
Podcast Ch21b Title: Collision Resolution
Podcast Ch23a Title: Bit Arrays
Podcast Ch23c Title: Binary Files
A type is a collection of values
Presentation transcript:

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

BitArray Class Implementation The BitArray class stores the bits in a byte 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)

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 = 01010011 y.set(5); System.out.println("set bit 5 in y = " + y); // set bit 5 in y = 01010111 System.out.println("shift x left 3 = " + x.shiftLeft(3)); // shift x left 3 = 10101000

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.

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; }

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.

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); }

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

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 = 11010101 System.out.println("y = " + y); // y = 01110011 System.out.println("x & y = " + x.and(y)); // x & y = 01010001 System.out.println("x | y = " + x.or(y)); // x | y = 11110111 System.out.println("~x = " + x.not()); // ~x = 00101010 NOTE: not used in podcast