|
1
|
- Instructor: Brian M. Dennis
- Teaching Assistants:
- Tom Lechner, Bin Lin, Rachel Goldsborough
- http://www.cs.northwestern.edu/~bmd/cs211/
|
|
2
|
- 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
|
- 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
|
- 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
|
- 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
|
- #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
|
- BitSet::BitSet() {
- bits = 0;
- }
- BitSet::add(int d) {
- int mask = 1 << d;
- bits |= mask;
- }
|
|
8
|
- 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
|
- #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
|
- #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
|
- // 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
|
- // 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
|
- // 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
|
- #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
|
- BitSet::BitSet() {
- bits = 0;
- }
- BitSet::add(int d) {
- int mask = 1 << d;
- bits |= mask;
- }
- BitSet& BitSet::operator+(int d) {
- add(d)
- return *this;
- }
|
|
16
|
- // 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
|
- Takeaways
- Bits can represent sets
- Building an ADT on that representation
- Operator overloading
- Reading 8.1 – 8.7
|