CS 211: Introduction to Computer Programming II - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

CS 211: Introduction to Computer Programming II

Description:

Integer is a sequence of 32 bits. dth bit indicates whether d is ... Use cautiously. Obscures behavior. Can actually implement opposite behavior for an operator ... – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 18
Provided by: briand8
Category:

less

Transcript and Presenter's Notes

Title: CS 211: Introduction to Computer Programming II


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

2
Todays Topics
  • Bitwise operators
  • Intro operator overloading
  • Quiz 1
  • Administrivia
  • Use office hours
  • Face to face clears things up a lot faster
  • Reading
  • 8.1 8.7

3
BitvectorRepresentation of Sets
  • Integer is a sequence of 32 bits
  • dth bit indicates whether d is in the set or not
  • Operations
  • Add to the set
  • Delete from the set
  • Check for membership
  • Union
  • Difference
  • Intersection

4
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

5
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 ltlt d
  • set set mask
  • set mask
  • set 1 ltlt d

6
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 ltlt d
  • int test set mask

7
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 ltlt d // mask 0100 0000
  • mask mask // mask 1011 1111
  • int test set mask

8
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

9
Set Abstract Datatype
  • BitSetBitSet()
  • bits 0
  • BitSetadd(int d)
  • int mask 1 ltlt d
  • bits mask

10
Set Abstract Datatype
  • BitSetdel()
  • int mask 1 ltlt d
  • mask mask
  • bits mask
  • bool BitSetis_member(int d)
  • int mask 1 ltlt d
  • int test bits mask
  • test ? true false

11
Set Abstract Datatype
  • include ltctimegt
  • include ltcstdlibgt
  • include "BitSet.h"
  • int main()
  • BitSet bit_set
  • for(int i 0 i lt 10 i)
  • int d rand() 32
  • bit_set.add(d)
  • return 0

12
Operator Overloading
  • Syntactic extension
  • Class mechanism
  • Let's us add new datatypes
  • But dont look like builtins
  • Method syntax isn't always natural

13
Set Abstract Datatype
  • include ltctimegt
  • include ltcstdlibgt
  • include "BitSet.h"
  • using namespace std
  • int main()
  • BitSet bit_set
  • srand(time(0))
  • for(int i 0 i lt 10 i)
  • int d rand() 32
  • bit_set d // Wouldn't it be nice if

14
Set Abstract Datatype
  • // Stuff elided
  • include ltiostreamgt
  • using namespace std
  • int main()
  • BitSet bit_set
  • // Stuff elided
  • // Let's print out a bitset
  • cout ltlt "BitSet"
  • for (int j 0 j lt 32 j)
  • if (bit_set.is_member(j))
  • cout ltlt j
  • cout ltlt ""
  • cout ltlt endl

15
Set Abstract Datatype
  • // Stuff elided
  • include ltiostreamgt
  • using namespace std
  • int main()
  • BitSet bit_set
  • // Stuff elided
  • // Let's print out a bitset
  • cout ltlt bit_set ltlt endl // Wouldn't it be nice

16
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 lt 10 i)
  • int d rand() 32
  • bit_set d // Wouldn't it be nice if

17
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

18
Operator Overloading
  • BitSetBitSet()
  • bits 0
  • BitSetadd(int d)
  • int mask 1 ltlt d
  • bits mask
  • BitSet BitSetoperator(int d)
  • add(d)
  • return this

19
Operator Overloading
  • // Has to be a standalone function not member
    function
  • ostream operatorltlt(ostream os, BitSet bs)
  • int foo 10
  • cin gtgt foo
  • return os
  • int main()
  • BitSet bit_set
  • // Stuff elided
  • // Let's print out a bitset
  • cout ltlt bit_set ltlt end // Wouldn't it be nice

20
Operator Overloading
  • What can be overloaded
  • Most operators
  • Can't do
  • . ., ?
  • Can't add any new ones
  • Can't change associativity
  • Can't change precedence
  • Can't change arity
  • Nothing done automatically for you
  • Issues
  • arity binary vs unary
  • operator side
  • Use cautiously
  • Obscures behavior
  • Can actually implement opposite behavior for an
    operator

21
Thats a Wrap
  • Takeaways
  • Bits can represent sets
  • Building an ADT on that representation
  • Operator overloading
  • Syntactic extension
  • Reading 8.1 8.7
Write a Comment
User Comments (0)
About PowerShow.com