You are on page 1of 40

One

major use of AND operation is for placing 0s in one part of a bit pattern while not disturbing the other part. For example 00001111

without knowing the second operand we can say that the first four most significant bits will be 0s Moreover the four least significant bits will be a copy of the second operand.

2-1

00001111 AND 10101010 00001010

2-2

This

use of the AND operation is called masking. Here one operand, called the mask, determines which part of the other operand will affect the result. In case of AND operation, masking produces a result that is a partial replica of the one operand, with 0s occupying the nonduplicated positions.

2-3

Such

an operation is useful when manipulating a bit map,


A string of bits in which each bit represents the presence or absence of a particular object We have encountered bit maps in context of representing images, where each bit is associated with a pixel.

2-4

Where

AND operation can be used to duplicate a part of bit string while placing 0s in the nonduplicated part The OR operation can be used to duplicate a part of a string while putting 1s in the nonduplicated part For example 11110000

Produces 1s in four most significant bits While remaining bits are copied in the four least significant bits.

2-5

11110000 OR 10101010 11111010

2-6

A major use of XOR operation is in the forming of complement of a bit string XORing any byte with a mask of all 1s produces the complement of the byte.

2-7

11111111 XOR10101010 01010101

2-8

Suppose

you want to isolate the middle 4 bits of a byte by placing 0s in the other 4bits without disturbing the middle 4bits. What mask must you use together with that operation?

00000000 00111100
1-9

00111100
Apply masking with?

AND Because in AND operations we put 0s where we dont want to change the bits.
1-10

Expressions / mixed expressions and casting (Implicit and Explicit casting), Post fix and Pre fix Operators Control structures. If, if-else, nested if,

How

expressions involving mixed types of data are evaluated.


int value1=10; long value2 =25L; float value3= 30.0f; double result=value1+value2+value3;

The

value of result is calculated as the sum of three different types of variables. For each add operation, one of the operands will be converted to the type of the other before addition can be carried out.
12

The

statement in the previous slide is actually executed with the steps:


value1+ value2 is calculated by converting value1 to type long before the addition. The result is also of type long, so the calculation is 10L+25L=35L The next operation is 35L+value3. The previous result, 35L is converted to type float so 35.0f+30.0f=65.0f Finally 65.0f is converted to double an stored in result.
13

The

rules for dealing with mixed expressions apply only when the types of the operands for a binary operations are different. Rules in the sequence in which they apply are as following:
If either operand is of type long double, the other us converted to long double. If either operand is of type double, the other us converted to double. If either operand is of type float, the other us converted to float.

14

Any operand of type char, signed char, unsigned char, short or unsigned short is converted to type int, as long as type int can represent all values of the original operand type. Otherwise, the operand is converted to unsigned int. An enumeration type is converted to the first int, unsigned int, long or unsigned long that accomodates the range of enum. If either operand is of type unsigned long, the other us converted to unsigned long.

15

If one operand is type long and the other is unsigned int, then provided type long can represent all values it is converted to type long otherwise unsigned long. If either operand is of type long, the other us converted to long.

16

The

basic idea is very simple. With two operands of different types the type with the lesser range of values is converted to the other. The formal rules roughly boil down to:
if the operation involves two different floating point types, the one with the lesser precision will be promoted to the other. If the operations involves an integer and a floating point value, the integer will be promoted to the floating point type. If the operation involves mixed integer types, the type with the more limited range will be promoted to the other. If the operation involves enumerations they will be converted to a suitable integer type.

17

The

term conversion/implicit casting means an automatic conversion of one type to another. The term promotion generally means a conversion of a data value from a type with lesser range to a type with a greater range. Explicitly conversion from one data type to another is referred to as cast and the action of explicitly converting a value is called casting.

18

short a=2000; int b; b = (int) a; // c-like cast notation b = int (a); // functional notation

19

Type casting Implicit casting

Expression float1 = 5; int1 = 3.8; int1 = 7; int2 = 2; float1 = int1 / int2 + 5; float2 = float(int1)/int2 +5

LHS Value 5.0 3

8.0 8.5

Both

the increment operator (++) and the decrement operator(--) come in two varieties:

Prefix and postfix. The prefix variety is written before the variable name (++myAge);

Variable is changed, then the expression it is in is evaluated. Expression the variable is in executes, then the variable is changed.

The postfix variety is written after(myAge++)

21

Increment

operator (++)

Increment variable by one c++

Same as c += 1

Decrement

operator (--) similar

Decrement variable by one c--

22

If

c = 5, then
cout << ++c;

c is changed to 6, then printed out

cout << c++;

Prints out 5 (cout is executed before the increment. c then becomes 6

23

24

25

control structures
Sequence structure

Programs executed sequentially by default

Selection structures

if, if/else, switch


while, do/while, for

Repetition structures

26

C++

keywords
C++ Keyw o rd s
Keywords common to the C and C++ programming languages auto continue enum if short switch volatile C++ only keywords asm delete inline private static_cast try wchar_t

Cannot be used as identifiers or variable names

break default extern int signed typedef while bool dynamic_cast mutable protected template typeid

case do float long sizeof union

char double for register static unsigned

const else goto return struct void

catch explicit namespace public this typename

class false new reinterpret_cast throw using

const_cast friend operator true virtual

27

Flowchart

Graphical representation of an algorithm Special-purpose symbols connected by arrows (flowlines) Rectangle symbol (action symbol)

Any type of action Beginning or end of a program, or a section of code (circles)

Oval symbol

Single-entry/single-exit

control structures

Connect exit point of one to entry point of the next


28

Selection

structure

Choose among alternative courses of action Pseudocode example:


If students grade is greater than or equal to 60 Print Passed

If the condition is true

Print statement executed, program continues to next statement Print statement ignored, program continues C++ ignores whitespace characters (tabs, spaces, etc.)
29

If the condition is false

Indenting makes programs easier to read

Translation

into C++

If students grade is greater than or equal to 60 Print Passed

if ( grade >= 60 ) cout << "Passed";


Diamond

symbol (decision symbol)

Indicates decision is to be made Contains an expression that can be true or false

Test condition, follow path

if

structure
30

Single-entry/single-exit

Flowchart

of pseudocode statement

31

if

Performs action if condition true actions if conditions true or false

if/else Different

Pseudocode

if students grade is greater than or equal to 60 print Passed else print Failed
C++

code

if ( grade >= 60 ) cout << "Passed"; else cout << "Failed";

32

Ternary conditional operator (?:) Three arguments (condition, value if true, value if false)
Code could be written:
cout << ( grade >= 60 ? Passed : Failed );

Condition

Value if true

Value if false

false

grade >= 60

true

print Failed

print Passed

Nested

if/else structures

if students grade is greater than or equal to 90 Print A else if students grade is greater than or equal to 80 Print B else if students grade is greater than or equal to 70 Print C else if students grade is greater than or equal to 60 Print D else Print F
34

One inside another, test for multiple cases Once condition met, other statements skipped

Example

if ( grade >= 90 ) cout << "A"; else if ( grade >= 80 ) cout << "B"; else if ( grade >= 70 ) cout << "C"; else if ( grade >= 60 ) cout << "D"; else cout << "F";

// 90 and above // 80-89 // 70-79 // 60-69

// less than 60

35

Compound

if ( grade cout << else { cout << cout << again.\n";

statement
>= 60 ) "Passed.\n";

Set of statements within a pair of braces

"Failed.\n"; "You must take this course

}
Without braces, cout << "You must take this course again.\n";

always executed
Block

Set of statements within braces


36

What

is wrong with the following if statement

if ( numNeighbors >= 3) { ++numNeighbors; cout << "You are dead!" << endl; } else --numNeighbors;
1-37

Describe

the output

int number = 4; double alpha = -1.0; if (number > 0) if (alpha > 0) cout << "Here I am!" << endl; else cout << "No, Im here!" << endl; cout << "No, actually, Im here!" << endl;
1-38

int number = 4; double alpha = -1.0; if (number > 0) { if (alpha > 0) cout << "Here I am!" << endl; else cout << "No, Im here!" << endl; } cout << "No, actually, Im here!" << endl; Output
No, Im here! No, actually, Im here!
1-39

Using

if-else statement write program that calculates the following:


Addition Subtraction Multiplication Division

When

a user writes addition is adds and so on

1-40

You might also like