View
230
Download
0
Category
Preview:
Citation preview
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
1/55
All the variables that we intend to use in a
program must have been declared with its
type specifier in an earlier point in the code.
Like we did in the previous code at thebeginning of the body of the function main when
we declared that a, b, and result were of type
int.
A variable can be either of global or local
scope.
1
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
2/55
A global variable is a variable declared in
the main body of the source code, outside
all functions.
A local variable is one declared within the
body of a function or a block.
2
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
3/55
3
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
4/55
Global variables can be referred from anywherein the code, even inside functions, whenever it isafter its declaration.
The scope of local variables is limited to theblock enclosed in braces ({}) where they aredeclared.
For example, if they are declared at the beginning ofthe body of a function (like in function main) theirscope is between its declaration point and the end of
that function.
In the example above, this means that if anotherfunction existed in addition to main, the localvariables declared in main could not be accessedfrom the other function and vice versa.
4
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
5/55
Constant Variables & Arithmetic,
logical, relational operators
Bitwise operators, Left shit, right
shift, OR, AND & XOR operations
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
6/55
6
C++ Data Types
Simple Structuredaddress
Integral enum Floating
char
short
int
longbool
signed
unsigned
float
double
long double
pointer
reference
array
structunion
class
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
7/557
char, short, int, long
Different sizes of integers - different memory size
Dependent upon the compiler
Integer values: Sequence of one or more digits
22 129 -67 0
commas are not allowed: 100,000
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
8/558
Type Name Bytes Other Names Range of Values
int 4 signed 2,147,483,648 to 2,147,483,647
unsigned int 4 unsigned 0 to 4,294,967,295
bool 1 none false or true
char 1 none 128 to 127
signed char 1 none 128 to 127
unsigned char 1 none 0 to 255
short 2 short int, signed short int 32,768 to 32,767
unsigned short 2 unsigned short int 0 to 65,535
long 4 long int, signed long int 2,147,483,648 to 2,147,483,647
unsigned long 4 unsigned long int 0 to 4,294,967,295
long long 8 none 9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
unsigned long long 8 none 0 to 18,446,744,073,709,551,615
enum varies none
float 4 none 3.4E +/- 38
double 8 none 1.7E +/- 308
long double same as double none same as double
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
9/559
C++ expressions are used to express
computation.
Expressions include operations and the
operands on which the operations areapplied.
Operands can be variables, literals or
constants.
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
10/5510
Precedence controls the order of evaluation
of operators.
A high precedence means an operator is
evaluated (applied) before any lower precedence
operators.
Operators that have the same precedence
can happen in either order, but in C++ the
one on the left is evaluated first.
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
11/5511
Operators Precedence() highest (applied first)
* / %
+ -
< >=
== !=
= lowest (applied last)
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
12/55
We can define any kind of variable as const.
The compiler will check that you dont
attempt to change the value of such a
variable.
12
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
13/55
#include
using namespace std;int main()
{
const int inches_per_foot=12;
const int feet_per_yard=3;
int yards=0;
int feet=0;
int inches=0;
cout yards >> feet >> inches;
cout
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
14/55
Enter a length as yards, feet, and inches :2 2 11
Length in inches is 107
14
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
15/5515
The compiler will complain if your code tries
to modify a const variable:
const int temp = 100;
temp = 21;
Error: l-value specifies const object
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
16/5516
Const tells the compiler that a variable
should never be changed.
You already know the variable should neverbe changed!
But - let the compiler save you from yourself
(you might forget that it shouldn't bechanged).
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
17/5517
You can add the const modifier to the
declaration of a variable to tell the compiler
that the value cannot be changed:
const double factor = 5.0/9.0;
const double offset = 32.0;
celcius = (fahr - offset)*factor;
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
18/5518
Arithmetic calculations *
Multiplication
/
Division
Integer division truncates remainder
7 / 5 evaluates to 1
%
Modulus operator returns remainder
7 % 5 evaluates to 2
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
19/5519
Rules of operator precedence
Operators in parentheses evaluated first
Nested/embedded parentheses
Operators in innermost pair first Multiplication, division, modulus applied next
Operators applied from left to right
Addition, subtraction applied last
Operators applied from left to right
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
20/55
20
Operator(s) Operation(s) Order of evaluation (precedence)
() Parentheses Evaluated first. If the parentheses are nested, the
expression in the innermost pair is evaluated first. If
there are several pairs of parentheses on the same level(i.e., not nested), they are evaluated left to right.
*, /, or% Multiplication Division
Modulus
Evaluated second. If there are several, they re
evaluated left to right.
+or- Addition
Subtraction
Evaluated last. If there are several, they are
evaluated left to right.
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
21/55
21
if structure
Make decision based on truth or falsity of
condition
If condition met, body executed
Else, body not executed
Equality and relational operators
Equality operators
Same level of precedence
Relational operators
Same level of precedence
Associate left to right
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
22/55
22
Sta nda rd a lge b ra ic
eq uality op erator or
relational op erator
C++ equality
or relat iona l
operator
Example
of C++
condition
Mea ning of
C++ cond ition
Relational operators
> > x > y x is greater than y
< < x < y x is less than y
>= x >= y x is greater than or equal to y
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
23/55
23
1 // code5.cpp
2 // Using if statements, relational
3 // operators, and equality operators.
4 #include
5
6 using std::cout; // program uses cout
7 using std::cin; // program uses cin
8 using std::endl; // program uses endl
9
10 // function main begins program execution
11 int main()
12 {
13 int num1; // first number to be read from user
14 int num2; // second number to be read from user
15
16 cout num1 >> num2; // read two integers
19 20 if( num1 == num2 )
21 cout
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
24/55
24
( )
27 cout
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
25/55
25
Enter two integers, and I will tell you
the relationships they satisfy: 7 7
7 is equal to 7
7 is less than or equal to 7
7 is greater than or equal to 7
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
26/55
26
Common error
Does not typically cause syntax errors
Aspects of problem
Expressions that have a value can be used fordecision
Zero = false, nonzero = true
Assignment statements produce a value (the
value to be assigned)
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
27/55
27
Example
if ( payCode == 4 )cout
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
28/55
As their name suggests, the bitwise operatorsenable you to operate on an integer variableat the bit level. Can be applied to any type of integers: Signed Unsigned
However they are usually applied to unsigned integertypes.
The bitwise operations are: SHIFT AND
OR
XOR
28
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
29/55
Provide a means for moving bits within a
register and are often used for solving
alignment problems.
One technique is to place the bit that fell offthe right end in the hole at the left end.
This is called circular shift or rotation.
2-29
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
30/55
2-30
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
31/55
Another technique is to discard the bit that
falls off the edge and always fill the hole
with a 0. This is called logical shift.
Shifts that leave the sign bit unchanged are
called arithmetic shifts.
2-31
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
32/55
A right circular shift of 3 bits on a string of
8bits is equivalent to a left circular shift of
how many times?
1-32
01100101
Applying first right circular shift:
_ 0 1 1 0 0 1 0 1
1 0 1 1 0 0 1 0
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
33/55
1-33
1 0 1 1 0 0 1 0
Applying 2nd right circular shift:
_ 1 0 1 1 0 0 1 0
0 1 0 1 1 0 0 1
Applying 3rd right circular shift:
_ 0 1 0 1 1 0 0 1
1 0 1 0 1 1 0 0
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
34/55
1-34
01100101
Now applying left rotation to find how many
Left rotations are equal to 3 right rotations
Applying first left circular shift:
0 1 1 0 0 1 0 1__
1 1 0 0 1 0 1 0
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
35/55
1-35
1 1 0 0 1 0 1 0
Applying 2nd left circular shift:
1 1 0 0 1 0 1 0 __1 0 0 1 0 1 0 1
Applying 3rd left circular shift:
1 0 0 1 0 1 0 1 _
0 0 1 0 1 0 1 1
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
36/55
So far if we compare the results of 3 right
shifts and 3 left shifts they are not equal:
3 right shifts :1 0 1 0 1 1 0 03 left shifts: 0 0 1 0 1 0 1 1
So we shall continue applying left shifts.
1-36
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
37/55
1-37
Applying 4th left circular shift:
0 0 1 0 1 0 1 1 _
0 1 0 1 0 1 1 0
Applying 5th left circular shift:
0 1 0 1 0 1 1 0 _
1 0 1 0 1 1 0 0
Now if we
compare theresults left shift
is equal to right
shift.
Hence 5 left shits
are equal to 3
right shifts.
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
38/55
They shift the contents of an integer variable
by a specified number of bits to the left or
right.
>> operator shifts bits to the right.
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
39/55
unsigned int number =163870;
unsigned int result= number
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
40/55
40
0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1
Similarly if we apply shift right:
Shift right 2:
0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
= 4096
unsigned int number =163870;unsigned int result= number >> 2;
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
41/55
number >> = 2;
This is equivalent to :
number = number >>2 ;cout
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
42/55
As mentioned earlier bitwise shift operators
can be applied to both signed and unsigned
integers.
Right shift on signed integer types can varybetween different systems and it depends on
your compiler.
In some cases it will fill 0 bits at the left to
fill in the vacated bit positions. In other cases the sign bit is propagated to
the right so 1 bit fills the vacated bit
positions.
42
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
43/55
The reason for propagating the sign bit,
where this occurs, is to maintain consistency
between a right shift and a divide operation.
We will illustrate this with a variable of typechar, just to show how it works.
43
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
44/55
signed char value= -104;
Which is the same as dividing -104 by 4 as we
would have expected.
44
Decimal -104 in binary :
1 0 0 1 1 0 0 0
value >> = 2;
1 1 1 0 0 1 1 0
= -26
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
45/55
Operator Description
- Bitwise complement operator. This is a unary operator
that will invert the bits to its operand so 1 becomes 0
and 0 becomes 1.
& Bitwise AND operator which will AND thecorresponding bits in its operands. If corresponding
bits are both 1 then resulting bit is 1, otherwise it is
0.
^ Bitwise exclusive OR operator (XOR) if corresponding
bits are different e.g. 1 and 0 result will be 1otherwise if bits are same e.g. 0 and 0 result will be
0.
| Bitwise OR operator. If corresponding bits are 0 and 0
the resulting bit will be 0, otherwise it is 1.
45
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
46/55
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-46
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
47/55
00001111
AND 1010101000001010
2-47
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
48/55
This use of the AND operation is called
masking.
Here one operand, called the mask,
determines which part of the other operandwill affect the result.
In case of AND operation, masking produces a
result that is a partial replica of the one
operand, with 0s occupying thenonduplicated positions.
2-48
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
49/55
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-49
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
50/55
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 apart 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-50
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
51/55
11110000
OR 1010101011111010
2-51
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
52/55
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-52
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
53/55
11111111
XOR1010101001010101
2-53
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
54/55
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?
1-54
00000000
00111100
7/31/2019 Lec10_Constant Variables & ALU, Bitwise
55/55
00111100Apply masking with?
AND
Because in AND operations we put0s where we dont want to change
the bits.
Recommended