C++ Programming
Ninevah University
College of Electronics Engineering
Department of Electronic Engineering
MEDICAL INSTRUMENTATION
2nd Year
2024 – 2025
10:30 – 12:30
Lecturer
Prof Dr. Qais Thanon
Lecture #2
All the lectures of this course will upload at the
Google classroom
9/24/2024
The cout Object:
Use the cout<< object to display information on the computer’s screen.
● Its job is to output information using the standard output device.
● The << operator is used to send the string like “NINEVAH
UNIVERSITY” to cout.
● cout does not produce a newline at the end of a statement
# include <iostream.h >
void main () {
cout << “ *** University of NINEVAH ***";
}
*** University of NINEVAH ***
The cin Object
• The cin>> object reads information types at the keyboard.
• Notice the >> and << operators appear to point in the direction
information is flowing.
2
9/24/2024
Arithmetic Operators
•There are many operators for manipulating numeric values and
performing arithmetic operations
3
9/24/2024
# include <iostream.h >
void main () {
int x; x = 7
x = 4 + 3; 7/3 = 2
cout << x / 3.0
3 <<<<
’ ’
“ “
<<<<
x x
* *
2;2; 7 * 2 =14
}
x = 7
Calculations can be performed in a output statement 7/3.0 = 2.33
7 * 2 =14
int / int = int
int / float = float
4
9/24/2024
Bitwise operators
C++ provides bitwise operators, which provide bit-level control.
The following list describes these operations:
& and | or
^ xor ~ not
>> left shift << right shift
5
9/24/2024
AND(&)
&
Number1 & Number2;
9 & 6 = 0 0 0 0 0 0
# include <iostream.h >
void main () { Let N1 = 7 , N2 = 3
int N1, N2;
N2, R;
0 1 1 1
cin >> N1 >> N2; & 0 0 1 1
<< &N1N2;
R = N1
cout & N2; =======
} cout << R; 0 0 1 1
}
6
9/24/2024
OR(|)
|
Number1 | Number2;
9 | 6 = 15 0 1 1 1 1
# include <iostream.h >
void main () { Let N1 = 7 , N2 = 3
int N1, N2;
N2, R;
0 1 1 1
cin >> N1 >> N2; |
0 0 1 1
<< |N1N2;
R = N1
cout | N2; =======
} cout << R; 0 1 1 1
}
7
9/24/2024
XOR(^)
^
Number1 ^ Number2;
9 ^ 6 = 15 0 1 1 1 1
# include <iostream.h >
void main () { Let N1 = 7 , N2 = 3
int N1, N2;
N2, R;
0 1 1 1
cin >> N1 >> N2; ^ 0 0 1 1
<< ^N1N2;
R = N1
cout ^ N2; =======
} cout << R; 0 1 0 0
}
8
9/24/2024
NOT(~)
~
~Number1
~9 = 22 1 0 1 1 0
# include <iostream.h >
void main () { Let N1 = 7
int N1, R;
~ 0 1 1 1
cin >> N1;
=======
R = ~ N1 ;
1 0 0 0
cout << R;
}
9
9/24/2024
Left Shift and Right Shift Operators in C++
Left Shift
A Left Logical Shift of one position moves each bit to the left by one. The
vacant least significant bit (LSB) is filled with zero and the most significant
bit (MSB) is discarded.
R = NUMBER << 1;
# include <iostream.h > 25 << 3
void main () {
int N1, R; 25 => 1 0 1 0 1
<<3
cin >> N1;
============
R = N1 << 3; 1 0 1 0 1 0 0 0
cout << R;
} 168
10
9/24/2024
Left Shift and Right Shift Operators in C++
Right Shift
A Right Logical Shift of one position moves each bit to the right by one. The
least significant bit is discarded and the vacant MSB is filled with zero.
R = NUMBER >> 1;
# include <iostream.h > 25 >> 3
void main () {
int N1, R; 25 => 1 0 1 0 1
>>3
cin >> N1;
============
R = N1 >> 3; 0 0 0 1 0 1 0 1
cout << R;
} 2
11
9/24/2024
Multiplication by left shift:
The result of a Left Shift operation is a multiplication by 2n , where n
is the number of shifted bit positions.
Example:
Let’s take the decimal number 2 represented as 8 bit binary
number 00000010. By shifting in to the left with one position we
get 00000100 which is 4 in decimal representation. If we shift it once
more we get binary value 00001000 which is 8 in decimal
representation.
2 0 0 0 0 0 0 1 0
2 << 1 = 4 0 0 0 0 0 1 0 0
4 << 1 = 8 0 0 0 0 1 0 0 0
2 << 1 2 2 4 << 1 4 2
12
9/24/2024
Division by right shift:
The result of a Right Shift operation is a division by 2n , where n is
the number of shifted bit positions.
Example:
If we have the binary number 01110101 (117 decimal) and we perform
arithmetic right shift by 1 bit we get the binary number 00111010 (58
decimal). So we have divided the original number by 2.
117 0 1 1 1 0 1 0 1
117 >> 1 = 58 0 0 1 1 1 0 1 0
58 >> 1 = 29 0 0 0 1 1 1 0 1
117 >> 1 117 / 2 58 >> 1 58 / 2
13
9/24/2024
#include <iostream.h>
void main() {
// a = 5(00000101), b = 9(00001001)
int a = 5, b = 9;
cout<<"a = " << a <<","<< " b = " << b;
cout << "a & b = " << (a & b);
// The result is 00000001
cout << "a | b = " << (a | b);
// The result is 00001101
cout << "a ^ b = " << (a ^ b);
// The result is 00001100
cout << "~(" << a << ") = " << (~a);
// The result is 11111010
cout<<"b << 1" <<" = "<< (b << 1);
// The result is 00010010
cout<<"b >> 1 "<<"= " << (b >> 1 );
// The result is 00000100 }
14
9/24/2024