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;

Slides:



Advertisements
Similar presentations
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Advertisements

Data Representation Computer Organization &
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
CS 151 Digital Systems Design Lecture 3 More Number Systems.
Data Representation COE 205
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
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.
ENGIN112 L3: More Number Systems September 8, 2003 ENGIN 112 Intro to Electrical and Computer Engineering Lecture 3 More Number Systems.
Bitwise Operations CSE 2451 Rong Shi. Working with bits – int values Decimal (not a power of two – used for human readability) – No preceding label –
Binary Arithmetic Math For Computers.
Abstraction – Number Systems and Data Representation.
1 Operator Overloading in C++ Copyright Kip Irvine, All rights reserved. Only students enrolled in COP 4338 at Florida International University may.
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.
10-Sep Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept Representing Information in Computers:  numbers: counting numbers,
Bit Manipulation when every bit counts. Questions on Bit Manipulation l what is the motivation for bit manipulation l what is the binary, hexadecimal,
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.
CSC 270 – Survey of Programming Languages C Lecture 5 – Bitwise Operations and Operations Miscellany.
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.
How a Computer Processes Information. Java – Numbering Systems OBJECTIVE - Introduction to Numbering Systems and their relation to Computer Problems Review.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Number Systems and Bitwise Operation.
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=
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
D75P 34R - HNC Computer Architecture Week 4 Signed Integers © C Nyssen/Aberdeen College 2004 All images © C Nyssen /Aberdeen College unless otherwise.
Chapter 1: Binary Systems
CSE 332: C++ template examples Today: Using Class and Function Templates Two examples –Function template for printing different types –Class template for.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Number Systems and Computer Arithmetic Winter 2014 COMP 1380 Discrete Structures I Computing Science Thompson Rivers University.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
Binary Addition The simplest arithmetic operation in binary is addition. Adding two single-digit binary numbers is relatively simple, using a form of carrying:
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.
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.
Lec 3: Data Representation
Data Representation.
Chapter 4 Operations on Bits.
Chapter 7: Expressions and Assignment Statements
Chap. 2. Types, Operators, and Expressions
Chapter 7: Expressions and Assignment Statements
Bit Operations Horton pp
Number Systems and Bitwise Operation
Formatting Output.
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.
Lesson 26 Miscellaneous Topics
Bits and Bytes Topics Representing information as bits
Coding Concepts (Data- Types)
CISC/CMPE320 - Prof. McLeod
Bitwise operators.
靜夜思 床前明月光, 疑是地上霜。 舉頭望明月, 低頭思故鄉。 ~ 李白 李商隱.
Bit Operations Horton pp
ECE 120 Midterm 1 HKN Review Session.
Presentation transcript:

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;

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

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 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* ?

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?

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; };

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

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*

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!)

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 C++ Annotations Version C/C++ Pointers

Decimal Numbers Humans Represent everything in decimal, 1 -> 10 Base 10 notation Each position is a power of * * * * 10 0 = = Base 10: count by 10’s

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

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 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?

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

Binary in C++ Why do we care? Binary describes size of data Integer stored in 32-bits, limited to ~5 billion values (or )  - ~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 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

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 << A >> B << B >> 20011

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