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/ | |
| 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 |
| 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; | |
| 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; | |
| 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; | |
| #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 | |
| BitSet::BitSet() { | |
| bits = 0; | |
| } | |
| BitSet::add(int d) { | |
| int mask = 1 << d; | |
| bits |= mask; | |
| } | |
| 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; | |
| } | |
| #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; | |
| } | |
| #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 | |
| } | |
| } | |
| // 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; | |
| } | |
| // 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 | |
| } | |
| // 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 | |
| } | |
| } | |
| #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 | |
| BitSet::BitSet() { | |
| bits = 0; | |
| } | |
| BitSet::add(int d) { | |
| int mask = 1 << d; | |
| bits |= mask; | |
| } | |
| BitSet& BitSet::operator+(int d) { | |
| add(d) | |
| return *this; | |
| } |
| // 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 | |
| } |
| Takeaways | ||
| Bits can represent sets | ||
| Building an ADT on that representation | ||
| Operator overloading | ||
| Syntactic extension | ||
| Reading 8.1 – 8.7 | ||