Lesson 26 Miscellaneous Topics

Slides:



Advertisements
Similar presentations
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Advertisements

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
Bit Operations C is well suited to system programming because it contains operators that can manipulate data at the bit level –Example: The Internet requires.
A bit can have one of two values: 0 or 1. The C language provides four operators that can be used to perform bitwise operations on the individual bits.
Bitwise Operations CSE 2451 Rong Shi. Working with bits – int values Decimal (not a power of two – used for human readability) – No preceding label –
Computers Organization & Assembly Language
CMSC 202 Lesson 23 Templates II. Warmup Write the templated Swap function _______________________________ void Swap( ________ a, ________ b ) { _______________.
Input & Output: Console
Lec 3: Data Representation Computer Organization & Assembly Language Programming.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
Copyright © Curt Hill BitWise Operators Packing Logicals with Other Bases as a Bonus.
Bitwise operators. Representing integers We typically think in terms of decimal (base 10) numbers.  Why?  A decimal (or base 10) number consists of.
Pointers OVERVIEW.
Bitwise operators. Representing integers We typically think in terms of decimal (base 10) numbers.  Why?  A decimal (or base 10) number consists of.
Bitwise Operators Fall 2008 Dr. David A. Gaitros
CSC 107 – Programming For Science. The Week’s Goal.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
Bit Operations Horton pp Why we need to work with bits Sometimes one bit is enough to store your data: say the gender of the student (e.g. 0.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Digital Representations ME 4611 Binary Representation Only two states (0 and 1) Easy to implement electronically %0= (0) 10 %1= (1) 10 %10= (2) 10 %11=
Number Systems and Computer Arithmetic Winter 2014 COMP 1380 Discrete Structures I Computing Science Thompson Rivers University.
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
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;
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Topic: Binary Encoding – Part 2
Bitwise Operations C includes operators that permit working with the bit-level representation of a value. You can: - shift the bits of a value to the left.
CSC 533: Programming Languages Spring 2016
Binary, Denary, Hexadecimal Conversion Binary Addition
Lec 3: Data Representation
CSC 533: Programming Languages Spring 2015
Chapter 4 Operations on Bits.
Chapter 7: Expressions and Assignment Statements
EGR 2261 Unit 10 Two-dimensional Arrays
Selection (also known as Branching) Jumail Bin Taliba by
Integer Real Numbers Character Boolean Memory Address CPU Data Types
Object Oriented Programming
Chapter 7: Expressions and Assignment Statements
EPSII 59:006 Spring 2004.
Variables and Primative Types
Bit Operations Horton pp
CISC/CMPE320 - Prof. McLeod
Number Systems and Bitwise Operation
What to bring: iCard, pens/pencils (They provide the scratch paper)
Templates II CMSC 202.
Data Representation Data Types Complements Fixed Point Representation
Introduction to Abstract Data Types
Andy Wang Object Oriented Programming in C++ COP 3330
CMSC 202 Lesson 22 Templates I.
Bitwise Operations C includes operators that permit working with the bit-level representation of a value. You can: - shift the bits of a value to the left.
Bits and Bytes Topics Representing information as bits
Coding Concepts (Data- Types)
Differences between Java and C
Homework Homework Continue Reading K&R Chapter 2 Questions?
CISC/CMPE320 - Prof. McLeod
Comp Org & Assembly Lang
Templates I CMSC 202.
Homework Finishing Chapter 2 of K&R. We will go through Chapter 3 very quickly. Not a lot is new. Questions?
Bitwise operators.
Lesson 25 Miscellaneous Topics
Lists CMSC 202, Version 4/02.
靜夜思 床前明月光, 疑是地上霜。 舉頭望明月, 低頭思故鄉。 ~ 李白 李商隱.
CSC 533: Programming Languages Spring 2019
Bit Operations Horton pp
ECE 120 Midterm 1 HKN Review Session.
Presentation transcript:

Lesson 26 Miscellaneous Topics 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; 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;

Announcements Last “regular” class day Review on Tuesday Homework Bring Review Questions! Project 4 Coming Soon! Project 5 Regrades ONLY if ABSOLUTELY necessary ONLY if it will affect your course grade

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

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

Pointer Dereferencing Problem: Imagine we want to create a templated Sort What if we have a collection of pointers? template < class T > void MySort( vector<T> &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<int*> 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<T> class What happens when we compare two items of type int* ?

Our Pointer<T> class template <class T> 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?

Overloading Pointer Dereferencing template <class T> 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; };

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

Overloading Conversion Operator template <class T> 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*

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

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

8 0 3 610 Decimal Numbers = 800010 + 3010 + 610 = 803610 Humans Represent everything in decimal, 1 -> 10 Base 10 notation Each position is a power of 10 8 0 3 610 Base 10: count by 10’s 8 * 103 0 * 102 3 * 101 6 * 100 = 800010 + 3010 + 610 = 803610

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 12 Base 2: count by 2’s 1 * 23 0 * 22 1 * 21 1 * 20 = 810 + 210 + 110 = 1110

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 232-1 values Two other common programming formats Octal – base 8 Has digits 0->7 Hexadecimal – base 16 Has digits 0->9 and A->F

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

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

Binary in C++ Why do we care? That’s great, but why do we REALLY care? Binary describes size of data Integer stored in 32-bits, limited to ~5 billion values (or 232-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!

Bit-wise Operators A 1010 B 1100 ~A 0101 ~B 0011 A & B 1000 A & A 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 A 1010 B 1100 ~A 0101 ~B 0011 A & B 1000 A & A A | B 1110 A | A

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 A 1010 B 1100 A ^ B 0110 A ^ A 0000 A << 1 0100 A >> 2 0010 B << 1 1000 B >> 2 0011

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

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

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

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?

Challenge Use bit-wise operators to implement binary addition