Andy Wang Object Oriented Programming in C++ COP 3330

Slides:



Advertisements
Similar presentations
1 Pointers Lecture Introduction Pointers  Powerful, but difficult to master  Simulate pass-by-reference  Close relationship with arrays and.
Advertisements

1 9/25/06CS150 Introduction to Computer Science 1 Nested Ifs, Logical Operators, exit() Page 194.
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
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.
1 Bitwise Operators. 2 Bits and Constants 3 Bitwise Operators Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise.
1 Bitwise Operators. 2 Bitwise Operators (integers) Bitwise "and" operator & Bitwise "or" operator | Bitwise "exclusive or" operator ^ Bitwise "ones complement"
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Bit Manipulation when every bit counts. Questions on Bit Manipulation l what is the motivation for bit manipulation l what is the binary, hexadecimal,
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. 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
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.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Number Systems and Bitwise Operation.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 25 December 1, 2009.
 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 5: Pointer Outline Chapter 5 Pointer continue Call by reference Pointer arithmatic Debugging.
Memory and the Character Display Chapter 10. The Bitwise Operators The bitwise operators are used to access the computer hardware. Use of bitwise operators.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
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;
ME2008– W05 MID1- Reference 2016Q1- Source: Deitel /C- How To.
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.
Today Variable declaration Mathematical Operators Input and Output Lab
Chapter 1.2 Introduction to C++ Programming
Chapter 3 Selection Statements
LESSON 06.
Andy Wang Object Oriented Programming in C++ COP 3330
Introduction to C++ (Extensions to C)
Chapter 3 Control Statements
Chapter 1.2 Introduction to C++ Programming
Data types Data types Basic types
Template Classes and Functions
Department of Computer and Information Science, School of Science, IUPUI Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI
Introduction to C++ Programming Language
Computing Fundamentals
CSC 113: Computer Programming (Theory = 03, Lab = 01)
Bit Operations Horton pp
CSC113: Computer Programming (Theory = 03, Lab = 01)
CSC113: Computer Programming (Theory = 03, Lab = 01)
Number Systems and Bitwise Operation
לולאות קרן כליף.
Object Oriented Programming COP3330 / CGS5409
Andy Wang Object Oriented Programming in C++ COP 3330
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
More about Numerical Computation
Operator Overloading.
Chapter 14 Bitwise Operators Objectives
אבני היסוד של תוכנית ב- C++
אבני היסוד של תוכנית ב- C++
Pointers & Functions.
Andy Wang Object Oriented Programming in C++ COP 3330
CISC181 Introduction to Computer Science Dr
Introduction to Programming
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
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Operator Overloading.
5.1 Introduction Pointers Powerful, but difficult to master
Operator Overloading.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Variables & Basic Types
Arithmetic Operations
Arrays Arrays A few types Structures of related data items
C++ Programming Lecture 18 Pointers – Part II
C++ Programming Basics
Pointers & Functions.
HNDIT11034 More Operators.
CISC181 Introduction to Computer Science Dr
4.1 Introduction Arrays A few types Structures of related data items
Bit Operations Horton pp
Presentation transcript:

Andy Wang Object Oriented Programming in C++ COP 3330 Bitwise Operators Andy Wang Object Oriented Programming in C++ COP 3330

Bits and Bytes A bit is the smallest unit of storage in a computer It store 0 or 1 A byte consists of 8 bits Smallest unit of directly addressable storage The smallest built-in data type is the char 1 byte on most systems What if we want to access individual bits? Must use the bitwise operators Caution: bitwise operators may be machine dependent Most machines use 2’s complement format

Base Conversion 1510 -> 11112 15 % 2 = 1 // least significant bit 15 / 2 = 7 7 % 2 = 1 // next bit 7 / 2 = 3 3 % 2 = 1 // next bit 3 / 2 = 1 1 % 2 = 1 // next bit 1 / 2 = 0 // terminate

Base Conversion 11112  1510 1x23 + 1x22 + 1x21 + 1x20 = 8 + 4 + 2 + 1 = 15

Two’s Complement Suppose we have 4-bit integers Non-negative numbers Just convert base 10 numbers to base 2 010 = 00002 110 = 00012 210 = 00102 To add two numbers, just add the corresponding bits 110 + 210 = 00012 + 00102 = 00112 = 1x21 + 1x20 = 310

Two’s Complement Negative numbers Flip all the bits of a non-negative numbers, then add 1 -110 = flip(00012) + 1 = 11102 + 1 = 11112 010 = flip(00002) + 1 = 11112 + 1 = 00002 If the uppermost bit is one, the number is negative To add a non-negative number with a negative number, just add 110 + (-1)10 = 00012 + 11112 = 00002 = 0

Two’s Complement To add two negative numbers, just add as well (-1)10 + (-1)10 = 11112 + 11112 = 11102 To decode a negative number, subtract the number by 1, and flip all the bits 11102 = negative flip(11102 – 1) = negative flip(11012) = negative 00102 = negative (1x21) = negative 210

The Bitwise Operators Operator Name Arity Description & Bitwise AND Binary Similar to the && operator, but on a bit-by-bit basis. | Bitwise OR Similar to the || operator, but on a bit-by-bit basis. ^ Bitwise Exclusive OR Set to 1 if one of the corresponding bits is 1, or set to 0 otherwise. ~ Complement Unary Flips the bits in the operand.

The Bitwise Operators Operator Name Arity Description << Left shift Binary Shifts the bits of the first operand to the left by the number of bits specified in the second operand. Right fill with 0 bits. >> Right shift Shifts the bits of the first operand to the right by the number of bits specified in the second operand. Left fill with 0’s for positive numbers, 1’s for negatives (machine dependent).

Shortcut Assignment Operators x &= y means x = x & y x |= y means x = x | y x ^= y means x = x ^ y x <<= y means x = x << y x >>= y means x = x >> y

Examples Suppose we have the following code short x = 6891; short y = 11318; Assume short is 2 bytes (16 bits) x: 00011010 11101011 y: 00101100 00110110 ---------------------- x & y:

Examples Suppose we have the following code short x = 6891; short y = 11318; Assume short is 2 bytes (16 bits) x: 00011010 11101011 y: 00101100 00110110 ---------------------- x & y: 00001000 00100010 (2082)

Examples ------------------------- x: 00011010 11101011 y: 00101100 00110110 ------------------------- x | y: x ^ y:

Examples ------------------------- x: 00011010 11101011 y: 00101100 00110110 ------------------------- x | y: 00111110 11111111 (16127) x ^ y:

Examples ------------------------- x: 00011010 11101011 y: 00101100 00110110 ------------------------- x | y: 00111110 11111111 (16127) x ^ y: 00110110 11011101 (14045)

Examples ------------------------- x: 00011010 11101011 ---------------------------- x << 2: y: 00101100 00110110 ------------------------- y >> 4: ~x:

Examples ------------------------- x: 00011010 11101011 ---------------------------- x << 2: 01101011 10101100 (27564) y: 00101100 00110110 ------------------------- y >> 4: ~x:

Examples ------------------------- x: 00011010 11101011 ---------------------------- x << 2: 01101011 10101100 (27564) y: 00101100 00110110 ------------------------- y >> 4: 00000010 11000011 (707) ~x:

Examples ------------------------- x: 00011010 11101011 ---------------------------- x << 2: 01101011 10101100 (27564) y: 00101100 00110110 ------------------------- y >> 4: 00000010 11000011 (707) ~x: 11100101 00010100 (-6892)

Code Examples http://www.cs.fsu.edu/~myers/cop3330/examples/bit wise/ex1.cpp

Code Examples #include <iostream> #include <iomanip> using std::cout; using std::endl; int main() { short x = 6891, y = 11318; cout << "x = " << x << "\ny = " << y << endl; cout << "x & y = " << (x & y) << endl; cout << "x | y = " << (x | y) << endl; cout << "x ^ y = " << (x ^ y) << endl; cout << "x << 2 = " << (x << 2) << endl; cout << "y >> 4 = " << (y >> 4) << endl; cout << "~x = " << ~x << endl; }

Examples from Dietel http://www.cs.fsu.edu/~myers/deitel5c++/ch22/Fig22_ 06/fig22_06.cpp Display bit values http://www.cs.fsu.edu/~myers/deitel5c++/ch22/Fig22_ 08/fig22_08.cpp &, |, ^, and ~ bitwise operators http://www.cs.fsu.edu/~myers/deitel5c++/ch22/Fig22_ 11/fig22_11.cpp << and >> bitwise operators

fig22_06.cpp #include <iostream> #include <iomanip> using namespace std; void displayBits(unsigned value) { const int SHIFT = 8*sizeof(unsigned) -1; const unsigned MASK = 1 << SHIFT; // 1000…. cout << setw(10) << value << “ = “; for (unsigned j = 1; j < SHIFT + 1; j++) { if (value & MASK) cout << ‘1’; value <<= 1; else cout << ‘0’; if (j % 8 == 0) cout << ‘ ‘; } cout << endl;

fig22_06.cpp int main() { unsigned inputValue; cout << “Enter an unsigned integer: “; cin >> inputValue; displayBits(inputValue); return 0; }

fig22_08.cpp #include <iostream> #include <iomanip> using namespace std; void displayBits(unsigned value) { const int SHIFT = 8*sizeof(unsigned) -1; const unsigned MASK = 1 << SHIFT; cout << setw(10) << value << “ = “; for (unsigned j = 1; j < SHIFT + 1; j++) { if (value & MASK) cout << ‘1’; value <<= 1; else cout << ‘0’; if (j % 8 == 0) cout << ‘ ‘; } cout << endl;

fig22_08.cpp int main() { unsigned number1, number2, mask, setBits; number1 = 179876355; mask = 1; cout << “The result of combining the following\n”; displayBits(number1); displayBits(mask); cout << “using the bitwise AND operator & is\n”; displayBits(number1 & mask); number1 = 15; setBits = 241; displayBits(number1); displayBits(setBits); cout << “using the bitwise OR operator | is\n”; displayBits(number1 | setBits);

fig22_08.cpp number1 = 139; number2 = 199; cout << “The result of combining the following\n”; displayBits(number1); displayBits(number2); cout << “using the bitwise exclusive OR operator ^ is\n”; displayBits(number1 ^ number2); number1 = 21845; cout << “\nThe one’s complement of\n”; displayBits(number1); cout << “is”; displayBits(~number1); return 0; }

fig22_11.cpp #include <iostream> #include <iomanip> using namespace std; void displayBits(unsigned value) { const int SHIFT = 8*sizeof(unsigned) -1; const unsigned MASK = 1 << SHIFT; cout << setw(10) << value << “ = “; for (unsigned j = 1; j < SHIFT + 1; j++) { if (value & MASK) cout << ‘1’; value <<= 1; else cout << ‘0’; if (j % 8 == 0) cout << ‘ ‘; } cout << endl;

fig22_11.cpp int main() { int number1 = -2000; cout << “The result of left shifting\n”; displayBits(number1); cout << “8 bit positions using the left-shift operator is\n”; displayBits(number1 << 8); cout << “\nThe result of right shifting\n”; cout << “8 bit positions using the right-shift operator is\n”; displayBits(number1 >> 8); return 0; }

BitFlags Examples http://www.cs.fsu.edu/~myers/cop3330/examples/bit wise/bitflags/ Set up to store a set of on/off flags On a system with 4-byte integers, a BitFlag object can store 32 flags (using one variable for all flags)

bitflags.h class BitFlags { public: BitFlags(); void Set(int num); void Unset(int num); void Flip(int num); bool Query(int num) const; private: int Mask(int num) const; unsigned int flags; const int numflags; };

bitflags.cpp #include <iostream> #include “bitflags.h” BitFlags::BitFlags() : numflags(sizeof(int)*8) { flags = 0; } int BitFlags::Mask(int num) const { return (1 << num); void BitFlags::Set(int num) { flags = flags | Mask(num);

bitflags.cpp void BitFlags::Unset(int num) { flags = flasg & ~Mask(num); } void BitFlags::Flip(int num) { flags = flags ^ Mask(num); Bool BitFlags::Query(int num) { return (flags & Mask(num));

main.cpp #include <iostream> #include “bitflags.h” using namespace std; int main() { BitFlags b; for (int j = 0; j < 32; j += 2) b.Set(j); for (int j = 0; j < 32; j++) // reverse order… cout << b.Query(j); cout << endl;

main.cpp for (int j = 0; j < 32; j++) cout << b.Query(j); b.Set(5); b.Unset(8); b.Flip(31); for (int j = 0; j < 32; j++) cout << b.Query(j); cout << endl; return 0; }