Operators are symbols used to be involved in the program to do surgery or manipulation.
example:
a = b + c * d
/ 4
a, b, c, d à disebut operand
=, +, *, / à disebut operator
Operator
|
Description
|
Example
|
+
|
Add |
m + n
|
-
|
Substract |
m – n
|
*
|
Multiply |
m * n
|
/
|
Divide |
m / n
|
%
|
Modulus |
m % n
|
-
|
Negat |
-m
|
Operators such as negation operator (-) is called a unary operator, as it requires only a single operand
example:
- #include <iostream>
- using namespace std;
- main()
- {
- int m = 82, n = 26;
- cout<<m<<" + "<<n<<" = "<<m+n<<endl;
- cout<<m<<" - "<<n<<" = "<<m-n<<endl;
- cout<<m<<" * "<<n<<" = "<<m*n<<endl;
- cout<<m<<" / "<<n<<" = "<<m/n<<endl;
- cout<<m<<" % "<<n<<" = "<<m%n<<endl;
- cout<<"-"<<m<<" = "<<-m<<endl;
- }
Output:
82 + 26 = 108
82 - 26 = 56
82 * 26 = 2132
82 / 26 = 3
82 % 26 = 4
-82 = -82
Because the data type is int, then 82/26 = 3, in order to represent the actual values, use the float data type.
Another way of writing using arithmetic operators:
m = m + n ó m += n
m = m - n ó m -= n
m = m * n ó m *= n
m = m / n ó m /= n
m = m % n ó m %= n
INCREMENT DAN DECREMENT OPERATOR:
Operator
increment à ++
Operator
decrement à --
Example:
- #include <iostream>
- using namespace std;
- main()
- {
- int m = 44, n = 66;
- cout<<”m = “<<m<<”, n = “<<n<<endl;
- ++m; --n;
- cout<<”m = “<<m<<”, n = “<<n<<endl;
- m++; n--;
- cout<<”m = “<<m<<”, n = “<<n<<endl;
- return 0;
- }
Output :
m = 44, n = 66
m = 45, n = 65
m = 46, n = 64
It appears that the operator pre-increment and post-increment has the same effect, namely the one at m manambah value and add it back to m (m = m +1). The same thing also happened on the carrier pre-decrement and post-decrement that gives the same result, which is to reduce the value of one of the n (n = n - 1).
But when used as a sub-expression, post-increment operator and pre-increment showed different results
example:
- #include <iostream>
- using namespace std;
- main()
- {
- int m = 66, n ;
- n = ++m;
- cout<<"m = "<<m<<", n = "<<n<<endl;
- n = m++;
- cout<<"m = "<<m<<", n = "<<n<<endl;
- cout<<"m = "<<m++<<endl;
- cout<<"m = "<<m<<endl;
- cout<<"m = "<<++m<<endl;
- return 0;
- }
Output:
m = 67, n = 67
m = 68, n = 67
m = 68
m = 69
m = 70
explanation:
In the first assignment, m is pre-increment, raising its value to 67, which is then put into n. In the second assignment, m is post-increment, so 67 is inserted into the first m n then its value is increased, which is why the value of m = 68 and n = 67. In the third assignment, m is post-increment, so that the value of m (= 68) displayed first (on the screen) and then the value of m raised to 69. In the fourth assignment, m is the pre-increment, so that the value of m is raised first to 70 and then displayed on the screen.
To be more aware, notice the example below.
- #include <iostream>
- using namespace std;
- main()
- {
- int m = 5, n;
- n = ++m * --m;
- cout<<"m = "<<m<<", n = "<<n<<endl;
- cout<<++m<<” “<<++m<<” “<<++m<<endl;
- return 0;
- }
Output:
m = 5, n = 25
8 7 6
explanation:
In assignment for n, m first increased (+ + m) to 6.
M then lowered back to 5, because of --m. So that m present value is 5 and the value of m = 5 is evaluated at the time of assignment multiplication done.
In the last line, the three sub-expressions are evaluated from right to left
BITWISE OPERATOR
Operator
|
Deskripsi
|
Contoh
|
<<
|
Geser n bit ke kiri ( left shift ) |
m << n
|
>>
|
Geser n bit ke kanan ( right shift ) |
m >> n
|
&
|
Bitwise AND |
m & n
|
|
|
Bitwise OR |
m | n
|
^
|
Bitwise XOR |
m ^ n
|
~
|
Bitwise NOT |
~m
|
Here is given the truth table for logical operators
P = A operator B
AND
|
OR
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
XOR
|
|||||||||||||||||||||||||||||||
|
Example:
- #include <iostream>
- using namespace std;
- main()
- {
- int m = 82, n = 26;
- cout<<m<<" << 2"<<" = "<<(m<<2)<<endl;
- cout<<m<<" >> 2"<<" = "<<(m>>2)<<endl;
- cout<<m<<" & "<<n<<" = "<<(m&n)<<endl;
- cout<<m<<" | "<<n<<" = "<<(m|n)<<endl;
- cout<<m<<" ^ "<<n<<" = "<<(m^n)<<endl;
- cout<<"~"<<m<<" = "<<~m<<endl;
- }
Output:
82 << 2
= 328
82 >> 2
= 20
82 & 26 =
18
82 | 26 = 90
82 ^ 26 = 72
~82 = 83
explanation:
Output value of the above, depending on the type of the compiler used. The results of the above is the output of the Turbo C + + compiler.
In Turbo C + + is an integer greater than or equal to 2 bytes of 16 bit, to know to use the command
cout << sizeof (int) << endl; / / To know of the int
Another way of writing using bitwise operators:
m = m
<< n ó m
<<= n
m = m
>> n ó m
>>= n
m = m & n ó m
&= n
m = m | n ó m
|= n
Example:
- #include <iostream>
- using namespace std;
- main()
- {
- int m = 5, n =7;
- if(m == n) cout<<m<<” same with ”<<n<<endl;
- else if(m != n) cout<<m<<” not same with ”<<n<<endl;
- else if(m > n) cout<<m<<” greater than ”<<n<<endl;
- else if(m < n) cout<<m<<” smaller than ”<<n<<endl;
- return 0;
- }
No comments:
Post a Comment