Nama : Ersus Saeful Hidayat
NIM : 49012050
JURUSAN : TMD (Teknik Media Digital) - STEI - ITB4. Operator Bagian 2 (matematika, logika, bitwise, ternary)
Operator Aritmatika digunakan untuk melakukan operasi aritmatika seperti penjumlahan, pengurangan, perkalian, pembagian dan sebagainya. Semua operator aritmatika berlaku untuk bilangan bulat (integer) maupun titik mengambang (float).
Source code :
/*==================================================================
Program Operator Aritmatika Microsoft Visual C++ 2012
Modul 4_1
Nama : Ersus
Saeful Hidayat
NIM :
49012050
Jurusan : TMD
(Teknik Media Digital)
==================================================================*/
#include <iostream>
using namespace std;
int main()
{
cout <<
" 2 + 3 =
";
cout <<
2 + 3;
cout <<
endl; cout << endl;
cout <<
" 10 - 5 =
";
cout <<
10 - 5;
cout <<
endl; cout << endl;
cout <<
" 4 x 3 =
";
cout <<
4 * 3;
cout <<
endl; cout << endl;
cout <<
" 4 / 2 =
";
cout <<
4 / 2;
cout <<
endl; cout << endl;
cout <<
" 10 % 3 =
";
cout <<
10 % 3;
cout <<
endl; cout << "\n";
system("pause");
return 0;
}
Operator Logika digunakan untuk
menghasilkan nilai benar (true) dan salah (false). Nilai ini dikenal dengan
istilah Boolean. Dalam C++, biasanya nilai benar direpresentasikan dengan nilai
1 dan salah direpresentasikan dengan nilai 0.
Source code :
/*==================================================================
Program Operator Logika : Defines the entry point for the
console application.
Microsoft Visual C++ 2012
Modul 4_2
Nama : Ersus
Saeful Hidayat
NIM :
49012050
Jurusan : TMD
(Teknik Media Digital)
==================================================================*/
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
std::cout
<< " OPERASI
OPERATOR LOGIKA \n";
cout <<
"\n Tabel
Kebenaran Operator AND \n";
cout <<
" 1 &&
1 = " << (1
&& 1) << endl;
cout <<
" 1 &&
0 = " << (1
&& 0) << endl;
cout <<
" 0 &&
1 = " << (0
&& 1) << endl;
cout <<
" 0 &&
0 = " << (0
&& 0) << endl;
cout <<
"\n Tabel
Kebenaran Operator OR \n";
cout <<
" 1 || 1 =
" << (1 || 1)
<< endl;
cout <<
" 1 || 0 =
" << (1 || 0)
<< endl;
cout <<
" 0 || 1 =
" << (0 || 1)
<< endl;
cout <<
" 0 || 0 =
" << (0 || 0)
<< endl;
cout <<
"\n Tabel
Kebenaran Operator NOT \n";
cout <<
" !1 = " << !1 << endl;
cout <<
" !0 = " << !0 << endl << "\n";
system("pause");
return 0;
}
Operator Bitwise berguna untuk melakukan operasi yang berhubungan dengan pemanipulasian bit. Operator bitwise hanya dapat dilakukan pada operand tipe char dan int saja, karena ini berkaitan secara langsung dengan tipe byte atau word didalam bit.
Source code :
/*==================================================================
Program Operator Bitwise Microsoft Visual C++ 2012
Modul 4_3
Nama : Ersus
Saeful Hidayat
NIM :
49012050
Jurusan : TMD
(Teknik Media Digital)
==================================================================*/
#include <iostream>
using namespace std;
int main()
{
int U, V, W;
U = 1
<< 1;
V = 1
<< 2;
W = 1
<< 3;
cout <<
"1 << 1
=" << U <<
endl;
cout <<
"1 << 2
=" << V <<
endl;
cout <<
"1 << 3
=" << W <<
endl << endl;
int X, Y, Z;
X = 16
>> 1;
Y = 16
>> 2;
Z = 16
>> 3;
cout <<
"16 >> 1
=" << X <<
endl;
cout <<
"16 >> 2
=" << Y <<
endl;
cout <<
"16 >> 3
=" << Z <<
endl << endl;
int A = 1;
int B = 0;
cout <<
"A = " << A << endl;
cout <<
"B = " << B << endl;
cout <<
"!A = " << !A << endl;
cout <<
"!B = " << !B << endl;
cout <<
"A & B =
" << (A &
B) << endl;
cout <<
"A | B = " << (A | B) << endl;
cout <<
"A ^ B = " << (A ^ B) << endl
<< endl;
system("pause");
return 0;
}
Operator Ternary adalah operator yang digunakan dalam operasi yang melibatkan tiga operand. Adapun operator yang digunakan untuk menyatakan adalah operator ?:. konsep yang mendasari operasi ini adalah suatu pencabangan (pemilihan) yang didasarkan pada kondisi tertentu.
Source code :
/*==================================================================
Program Operator Ternary : Defines the entry point for
the console application.
Microsoft Visual C++ 2012
Modul 4_4
Nama : Ersus
Saeful Hidayat
NIM :
49012050
Jurusan : TMD
(Teknik Media Digital)
==================================================================*/
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int X;
cout <<
" Masukan nilai
X = ";
cin >>
X;
cout <<
"\n";
X = (X <
0) ? - X : X;
cout <<
" | X | =
" << X;
cout <<
"\n \n";
system("pause");
return 0;
}
Terimakasih, Semoga dapat bermanfaat.
No comments:
Post a Comment