Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMSC 202 Lesson 26 Miscellaneous Topics. Warmup Decide which of the following are legal statements: int a = 7; const int b = 6; int * const p1 = & a;

Similar presentations


Presentation on theme: "CMSC 202 Lesson 26 Miscellaneous Topics. Warmup Decide which of the following are legal statements: int a = 7; const int b = 6; int * const p1 = & a;"— Presentation transcript:

1 CMSC 202 Lesson 26 Miscellaneous Topics

2 Warmup Decide which of the following are legal statements: int a = 7; const int b = 6; int * const p1 = & a; int * const p2 = & b; const int * p3 = & a; const int * p4 = & b; const int * const p5 = & a; const int * const p6 = & a; p1 = & a; p3 = & a; p5 = & a;

3 Today Overload the dereferencing operator Weirdness of the * operator! Bit-wise operators (important for 341) Binary representation Binary addition Bit-masking ~&, |, ^, >

4 Disclaimer The material I am about to present is an advanced concept from 341 The 341 book (Weiss) actually has it WRONG! Short write-up with some good code Linked from the Slides webpage Topic: Overloading Pointer Dereferencing Overloading Conversion Operator Oooo, Aaaah

5 Pointer Dereferencing Problem: Imagine we want to create a templated Sort What if we have a collection of pointers? template void MySort( vector &collection ) { /* code that sorts collection has something like: */ if (collection.at(i) < collection.at(j)) { swap(collection.at(i), collection.at(j)); } } // In main… vector vec; srand(0); for (int i = 0; i < 1000; ++i) vec.push_back(new int(rand())); Solution: We already saw auto_ptr Roll our own Pointer class What happens when we compare two items of type int* ?

6 Our Pointer class template class Pointer { public: Pointer(T *rhs = NULL ) : pointee(rhs) {} bool operator<( const Pointer & rhs ) const { return *pointee < *rhs.pointee; } private: T* pointee; }; What if we want to print the pointee? What if we want to change its value?

7 Overloading Pointer Dereferencing template class Pointer { public: Pointer(T *rhs = NULL ) : pointee(rhs) {} bool operator<( const Pointer & rhs ) const { return *pointee < *rhs.pointee; } // Pointer dereferencing operator const T operator * () const { return *pointee; } private: T* pointee; };

8 Using the Pointer Dereference template ostream& operator p) { sout << *p << endl; return sout; } Dereferencing a class that overloads the pointer dereferencing operator – calls that method! “Smart” pointers

9 Overloading Conversion Operator template class Pointer { public: Pointer(T *rhs = NULL ) : pointee(rhs) {} bool operator<( const Pointer & rhs ) const { return *pointee < *rhs.pointee; } // Conversion operator operator const T * () const { return pointee; } private: T* pointee; }; This looks very similar… What’s the difference? Position of the word ‘operator’ Operator name is: const T*

10 Differences… // Pointer dereferencing operator const T operator * () const { return *pointee; } // Conversion operator operator const T * () const { return pointee; } Conversion Converts something of type Pointer into something of type const T* (before dereferencing the data member!) Dereferencing Returns an object of type T (after dereferencing the data member!)

11 Final Notes about * If both dereferencing and conversion are overloaded… Dereferencing operator takes precidence (put in some cout statements to verify this!) Conversion operator Can be used to convert between ANY two types! Cool! Good examples in below material Additional Resources ANSI/ISO C++ Professional Programmer's Handbook http://www-f9.ijs.si/~matevz/docs/C++/ansi_cpp_progr_handbook/ch03/ch03.htm#Heading12 C++ Annotations Version 6.1.2 http://www.icce.rug.nl/documents/cplusplus/cplusplus09.html#l144 C/C++ Pointers http://uvsc.freshsources.com/Operator_Overloading.ppt

12 Decimal Numbers Humans Represent everything in decimal, 1 -> 10 Base 10 notation Each position is a power of 10 8 0 3 6 10 8 * 10 3 0 * 10 2 3 * 10 1 6 * 10 0 = 8000 10 + 30 10 + 6 10 = 8036 10 Base 10: count by 10’s

13 Binary Numbers Computers Represent everything in binary, 1’s and 0’s Base 2 notation Each position is a power of 2 1 0 1 1 2 1 * 2 3 0 * 2 2 1 * 2 1 1 * 2 0 = 8 10 + 2 10 + 1 10 = 11 10 Base 2: count by 2’s

14 Binary Numbers Usually represented in sets of 4 digits 4, 8, 16, 32, etc. Bit Binary digit Byte Collection 8-bits Integers stored in 4 bytes or 32 bits 32-bits Can represent up to 2 32 -1 values Two other common programming formats Octal – base 8 Has digits 0->7 Hexadecimal – base 16 Has digits 0->9 and A->F

15 Binary Representations What decimal equivalent are the following binary numbers? 0001 0100 1000 1001 1100 1111 0101

16 Binary Addition Just like decimal addition Except 1 2 + 1 2 == 10 2 Carry a 1 when you add two or more 1’s Let’s try a simple one… 1001 + 0011 11 0011 In decimal? 9 + 3 12 We leave off base subscript if the context is clear…

17 Binary in C++ Why do we care? Binary describes size of data Integer stored in 32-bits, limited to ~5 billion values (or 2 32 -1)  - ~2.7 billion -> ~2.7 billion That’s great, but why do we REALLY care? Assume lots of boolean values… Can use each bit to represent a separate value! Compress data, optimize data access Get at “raw” data Look for these again in Hash Tables!

18 Bit-wise Operators Operate on each bit individually…. ~ Bit-wise not 1 becomes 0 0 becomes 1 & Bit-wise logical and 1 if both 1 0 otherwise | Bit-wise logical or 1 if either or both 1 0 otherwise A1010 B1100 ~A0101 ~B0011 A & B1000 A & A1010 A | B1110 A | A1010

19 More Bit-wise Operators ^ Bit-wise exclusive or 1 if either but not both 1 0 otherwise << N Bit-wise left shift Moves all bits to the left N places Shifts on a zero on the right Left-most bit(s) discarded >> N Bit-wise right shift Moves all bits to the right N places Shifts on a zero on the left Right-most bit(s) discarded A1010 B1100 A ^ B0110 A ^ A0000 A << 10100 A >> 20010 B << 11000 B >> 20011

20 Bit-wise Compound Assignment &= & and assign |= | and assign ^= ^ and assign <<= << and assign >>= >> and assign

21 Bit Masking So, have a bunch of boolean values to store Often called “flags” Need two things: Variable to store value in Bit-mask to “retrieve” or “set” the value Use characters – unsigned value char flags = 0;// binary: 0000 char flag4 = 8;// binary: 1000 char flag3 = 4;// binary: 0100 char flag2 = 2;// binary: 0010 char flag1 = 1;// binary: 0001

22 Bit Masking Operations // Set flag1 to “true” flags = flags | flag1;// 0000 | 0001 // Set flag1 to “false” flags = flags & ~flag1;// 0001 & 1110 // Set several flags to “true” flags = flags | flag1 | flag3; // 0000 | 0001 | 0100 // Set all flags to “false” flags = flags ^ flags;// 0101 ^ 0101 // Set to a specific value flags = 11;// 1011 // Set all flags to “true” flags = flags | ~flags;// 1011 | 0100

23 Practice Convert the following decimal digits into binary 7, 5 Add them together using binary addition Check your result using decimal What about these two numbers? 9, 13 Use only 4-bits to represent these numbers What about negative numbers?

24 Challenge Use bit-wise operators to implement binary addition


Download ppt "CMSC 202 Lesson 26 Miscellaneous Topics. Warmup Decide which of the following are legal statements: int a = 7; const int b = 6; int * const p1 = & a;"

Similar presentations


Ads by Google