Notes
Slide Show
Outline
1
CS 211:
Introduction to Computer Programming II
  • Instructor: Brian M. Dennis
  • Teaching Assistants:
  • Tom Lechner, Bin Lin, Rachel Goldsborough
  • http://www.cs.northwestern.edu/~bmd/cs211/


2
Binary Reping Sets
  • int set = 0x5A; // Binary 0101 1010, decimal 90


  • // Integers in the set, 1 3 4 6


  • // Technically, can hold 32 integers
  • // 0 to 31
  • // 0000 0000 0000 0000 0000 0000 0101 1010


  • // Another set
  • int another_set = 0xdecafbad;
  • // 1101 1110 1100 1010 1111 1011 1010 1101
3
Adding an Element
  • int set = 0x5A; // Binary 0101 1010, decimal 90


  • // Adding a an integer d to the set
  • // Left shift, bitwise or
  • int d = 7;
  • int mask = 1 << d;
  • set = set | mask;
  • set |= mask;
  • set |= 1 << d;


4
Membership Testing
  • int set = 0x5A; // Binary 0101 1010, decimal 90


  • // Testing membership of int d in the set
  • // Left shift, bitwise and
  • int d = 7;
  • int mask = 1 << d;
  • int test = set & mask;




5
Deleting a Member
  • int set = 0x5A; // Binary 0101 1010, decimal 90


  • // Deleting d from the set
  • // Left shift, negate, bitwise and
  • int d = 6;
  • int mask = 1 << d;  // mask = 0100 0000
  • mask = ~mask;       // mask = 1011 1111
  • int test = set & mask;




6
Set Abstract Datatype
  • #ifndef _SET_H_
  • #define _SET_H_ 1


  • class BitSet {
  • public:
  • BitSet();


  • void add(int d);
  • void del(int d);
  • bool is_member(int d);
  • public:
  • int bits;
  • };
  • #endif


7
Set Abstract Datatype
  • BitSet::BitSet() {
  • bits = 0;
  • }


  • BitSet::add(int d) {
  • int mask = 1 << d;
  • bits |= mask;
  • }




8
Set Abstract Datatype
  • BitSet::del() {
  • int mask = 1 << d;
  • mask = ~mask;
  • bits &= mask;
  • }


  • bool BitSet::is_member(int d) {
  • int mask = 1 << d;
  • int test = bits & mask;
  • test ? true : false;
  • }



9
Set Abstract Datatype
  • #include <ctime>
  • #include <cstdlib>
  • #include "BitSet.h"


  • int main() {
  • BitSet bit_set;
  • for(int i = 0; i < 10; i++) {
  • int d = rand() % 32;
  • bit_set.add(d);
  • }
  • return 0;
  • }


10
Set Abstract Datatype
  • #include <ctime>
  • #include <cstdlib>
  • #include "BitSet.h"
  • using namespace std;


  • int main() {
  • BitSet bit_set;
  • srand(time(0));
  • for(int i = 0; i < 10; i++) {
  • int d = rand() % 32;
  • bit_set + d;  // Wouldn't it be nice if
  • }
  • }


11
Set Abstract Datatype
  • // Stuff elided
  • #include <iostream>
  • using namespace std;
  • int main() {
  • BitSet bit_set;
  • // Stuff elided
  • // Let's print out a bitset
  • cout << "BitSet[";
  • for (int j = 0; j < 32; j++) {
  • if (bit_set.is_member(j)) {
  • cout << j;
  • }
  • }
  • cout << "]";
  • cout << endl;
  • }


12
Set Abstract Datatype
  • // Stuff elided
  • #include <iostream>
  • using namespace std;
  • int main() {
  • BitSet bit_set;
  • // Stuff elided
  • // Let's print out a bitset
  • cout << bit_set << endl;  // Wouldn't it be nice


  • }


13
Operator Overloading
  • // Stuff elided
  • BitSet& operator+(BitSet& bs, int d) {
  • bs.add(d);
  • return bs;
  • }


  • int main() {
  • BitSet bit_set;
  • for(int i = 0; i < 10; i++) {
  • int d = rand() % 32;
  • bit_set + d;  // Wouldn't it be nice if
  • }
  • }


14
Operator Overloading
  • #ifndef _SET_H_
  • #define _SET_H_ 1


  • class BitSet {
  • public:
  • BitSet();


  • void add(int d);
  • void delete(int d);
  • bool is_member(int d);
  • // Let's hook into C++'s syntax
  • BitSet& operator+(int d);
  • private:
  • int bits;
  • };
  • #endif


15
Operator Overloading
  • BitSet::BitSet() {
  • bits = 0;
  • }


  • BitSet::add(int d) {
  • int mask = 1 << d;
  • bits |= mask;
  • }


  • BitSet& BitSet::operator+(int d) {
  • add(d)
  • return *this;
  • }
16
Operator Overloading
  • // Has to be a standalone function not member function
  • ostream& operator<<(ostream& os, BitSet& bs) {
  • int foo = 10;
  • cin >> foo;
  • return os;
  • }


  • int main() {
  • BitSet bit_set;
  • // Stuff elided
  • // Let's print out a bitset
  • cout << bit_set << end;  // Wouldn't it be nice


  • }
17
That’s a Wrap
  • Takeaways
  • Bits can represent sets
  • Building an ADT on that representation
  • Operator overloading
    • Syntactic extension
  • Reading 8.1 – 8.7