Professional Documents
Culture Documents
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
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
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
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
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
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
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
Expression float1 = 5; int1 = 3.8; int1 = 7; int2 = 2; float1 = int1 / int2 + 5; float2 = float(int1)/int2 +5
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.
21
Increment
operator (++)
Same as c += 1
Decrement
22
If
c = 5, then
cout << ++c;
23
24
25
control structures
Sequence structure
Selection structures
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
break default extern int signed typedef while bool dynamic_cast mutable protected template typeid
27
Flowchart
Graphical representation of an algorithm Special-purpose symbols connected by arrows (flowlines) Rectangle symbol (action symbol)
Oval symbol
Single-entry/single-exit
control structures
Selection
structure
Print statement executed, program continues to next statement Print statement ignored, program continues C++ ignores whitespace characters (tabs, spaces, etc.)
29
Translation
into C++
if
structure
30
Single-entry/single-exit
Flowchart
of pseudocode statement
31
if
if/else Different
Pseudocode
if students grade is greater than or equal to 60 print Passed else print Failed
C++
code
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";
// less than 60
35
Compound
statement
>= 60 ) "Passed.\n";
}
Without braces, cout << "You must take this course again.\n";
always executed
Block
What
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
When
1-40