Termux Work & Programming Language

Termux Work & Programming Language
Learn C++ #06: Six Types of Operators You Should Know in C++



In the previous tutorial we have learned about variables and data types.


For those of you who have not read it, I recommend reading it before following this tutorial.


Please open: Variables and Data types on C++


Thanks to variables we can store data in the program. But so far, we haven't done any surgery on him.


It's free to keep the data, but it's not processed.


Well, to process it we need an operator.


What is Operator?


and how to use it?


Let's discuss…


What is Operator?


The operator is a symbol…


Symbols used to perform a specific operation.


For example:


We want to sum the values of the variables x and y, then we can use the addition operator (+).


x + y's


There are six types of carrier groups in C++ programming that you should know:


Artimatic Operator;


Assignment Operator;


Comparator Operator;


Logic Operator;


Bitwise Operator;


and Other Operators.


What are the differences between all these types of operators?


Let's discuss, one by one…



Arithmetic Operator



An arithmetic operator is an operator used to perform arithmetic operations.


This operator consists of:


Operator Name


Summations. +


Reduction -.


Multiplications. *


Parceling. /


Remainder Divide. %


How to wear it?


Let's try in the example program:


Create a new program called operator_aritmatika.cpp, then fill it with the following code.


#includes


using namespace std;


int main(){


int a, b, c;


cout << "Input value a: ";


cin >> a;


cout << "Input value b: ";


cin >> b;


// using addition operator


c \= a + b;


cout << "Result a + b \= " << c << endl;


return 0;


}


The result: then:


Try also for examples of other operators such as subtraction, multiplication, division, and remainder for.


Turn the program into this:


#includes


using namespace std;


int a, b;


cout << "Input value a: ";


cin >> a;


cout << "Input value b: ";


cin >> b;


cout << "Result a + b: " << a +b << endl;


cout << "Result a - b: " << a -b << endl;


cout << "Result a *b: " << a *b << endl;


cout << "Result a /b: " << a /b << endl;


cout << "Result a %b: " << a %b << endl;


return 0;


}


The result: then:


In a division operation, 7/2 results in 3.


Why so?


It shouldn't be 3.5…


This is because we perform operations against integer data types.


When we change the program it uses float data type:


#includes


using namespace std;


int main(){


cout << "Input value a: ";


cin >> a;


cout << "Input value b: ";


cin >> b;


cout << "Result a /b: " << a /b << endl;


return 0;


}


The result will be like this:



Assignment Operator



Assignment operator (Assignment Operator) is an operator to assign tasks to variables. Usually to fill in the value.


Assignment Operators consist of:


Operator Name. Buttons


Charging Value. \=


Charging and Addition. +\=


Charging and Reduction. -\=


Charging and Multiplication. *\=


Charging and Dividing. /\=


Charging and Rest for %\=


Charging and shift left. <<\=


Charging and shift right. >>\=


Charging and bitwise AND. &\=


Charging and bitwise OR. |\=


Charging and bitwise XOR. ^\=


Let's try it in the program…


Create a new program called operator_assignment.cpp, then fill it with the following code:


#includes


using namespace std;


int main(){


int a, b;


// filling value with operator \=


a \= 5;


b \= 10;


// charging at once addition


b +\= a; // this is the same as b \= b + a


cout << "Result b +\= a is "<< b << endl;


/ charging at once reduction


b *\= a; // this is the same as b \= b * a


cout << "Result b -\= a is "<< b << endl;


// filling at once multiplication


b *\= a; // this is the same as b \= b * a


cout << "Result b *\= a is "<< b << endl;


// filling at once division


b /\= a; // this is the same as b \= b/a


cout << "Result b /\= a is "<< b << endl;


// charging at once addition


b %\= a; // this is the same as b \= b % a


cout << "Result b %\= a is "<< b << endl;


return 0;


}


Result:


In the program, variable b we refill with the assignment operator.


For example, the operation:


b +\= a


Just like the operation:


b \= b + a


This means that we will fill the value for b with the value of b plus the value of a.


Same with other opeartors.


b *\= a; // -> b \= b * a


Fill b with the value of the multiplication b with a.


The point is, what you need to remember:


“assignment operator used to fill value.”



Comparator Operator



The comparison operator is the operator to assign two values. This operator is also known as a relation operator.


Comparator operators consist of:


Operator Name. Symbols


Greater than. >


Smaller. <


Equal To. \=\=


Not Same as. !\=


Greater Same as. >\=


Smaller Same as. <\=


The values resulting from the comparison operation will be true and false.


In C++, true values will be equal to 1 and false will be equal to 0.


Let's try it in the program…


Create a new program with the name operator_comparator.cpp, then fill it with the following code:


#includes


using namespace std;


int main(){


int a \= 4, b \= 3;


bool results;


cout << "a \= " << a << endl;


cout << "b \= " << b << endl;


// using comparison operators


result \= a > b;


cout << "a > b \= " << result << endl;


result \= a < b;


cout << "a < b \= " << result << endl;


result \= a >\= b;


cout << "a >\= b \= " << result << endl;


result \= a <\= b;


cout << "a <\= b \= " << result << endl;


result \= a \=\= b;


cout << "a \=\= b \= " << result << endl;


results \= a !\= b;


cout << "a !\= b \= " << result << endl;


return 0;


}


Comparison operators will usually be used when making branching.



Logic Operator



If you have ever learned mathematical logic, you will certainly not be familiar with this operator.


Operator Name. Symbols


Logic AND. &&


Logic OR. | |


Negation/reverse. !


Logic operators are used to create logical operations.


Like this one: for example:


Statement 1: mr.website 7a programmer


Pernyattan 2: mr.website 7 using Linux


If asked, is Farmer Code a programmer who uses Linux?


Of course we'll check the truth first


Statement 1: mr.website 7a programmer\=true


Download 2: mr.website 7 using Linux\=true


What is Mr.website7 programmer and use Linux?


Statement 1 && Statement 2 \= true


Confused?


Try checking the truth table again for AND logic.


Difficulty 1 Ingestion 2 Results Logic AND


true. true.


true. false.


false. true.


false. false.


Still confused…?


Looks like you have to open again math logic lesson 😄.


How are the users in the program?


Let's look at an example…


Create a new program called operator_logika.cpp, then this with the following code.


#includes


using namespace std;


int main(){ int a \= 1; // true


int b \= 0; // false


bool results;


cout << "a \= " << a << endl;


cout << "b \= " << b << endl;


// AND logic


result \= a && b;


cout << "a && b \= " << result << endl;


// logic OR


result \= a || b;


cout << "a || b \= " << result << endl;


// logic NOT


cout << "!a \= " << !a << endl;


return 0;


}


Result:



Bitwise Operator



A bitwise operator is an operator used for operations based on the bit (binary) of a value.


Bitwise operators consist of:


Name of Operator Symbol in Java


AND. &


OR'S. |


XOR. ^


NOT/complement. ~


Left Shifts. <<


Shift Right. >>


For those of you who have studied number systems and digital systems will easily understand how this operator works.


But for those of you who haven't…


I will try to explain it in an easy way.


All righty!


Suppose we have 6 and 3.


The values 6 and 3 are then converted into binary numbers.


The whole three will be like this


6 \= 0110


3 \= 0011


Well, bitwise operators will perform operations based on those binaries.


Let's look at the first example:


Bitwise AND (&)


Bitwise AND is a bit operation based on AND logic


Bitwise AND



& 3


0110


0011


___________ &


0010 \= 2



Note the binary number for values 6 and 3.


If we do an AND operation there, it will generate a new binary number.


Then the resulting binary is converted back into decimal numbers.


The result is 2.


Let's try it in the program.


Create a new program called bitwise_and.c, and then fill it with the following code:


#includes


using namespace std;


int main(){


int a \= 6;


int b \= 3;


int results;


// using bitwise and operator


result \= a & b;


cout << "a & b \= " << result << endl;


return 0;


}


The result: then:


Bitwise OR (|)


The bitwise OR operator is also the same as bitwise AND.


The bitwise OR operator will generate a false value or 0 when both are false.


Bitwise OR



|. 3



0110


0011


___________ |


0111 \= 7


Program Example: bitwise_or.c


#includes


using namespace std;


int main(){


int a \= 6;


int b \= 3;


int results;


// using bitwise or operator


result \= a | b;


cout << "a & b \= " << result << endl;


return 0;


}


The output:


a | b \= 7


Bitwise XOR (^)


The XOR operator (Exclusive OR) will produce a value of 1 when the two values are not equal.


Bitwise XOR



6. ^. 3



0110


0011


\_\_\_\_\_\_\_\_\_\_\_^


Program Example: bitwise_xor.c


#includes


using namespace std;


int main(){


int a \= 6;


int b \= 3;


int results;


// uses xor bitwise operator


result \= a ^ b;


cout << "a ^ b \= " << result << endl;


return 0;


}


The output:


a ^ b \= 5


Bitwise NOT (~)


Bitwise NOT is also known as complement.


This operator will generate an inverse binary value from the original binary.


Then represented by two components.


Program example: bitwise_not.c


#includes


using namespace std;


int main(){


int a \= 6;


int results;


// using bitwise operator not


result \= ~a;


cout << "~a \= " << result << endl;


return 0;


}


Farmer Code


 Ahmad Muhardian 20 Dec 2019


Learn C++ #06: Six Types of Operators You Should Know in C++


#C++



In the previous tutorial we have learned about variables and data types.


For those of you who have not read it, I recommend reading it before following this tutorial.


Please open: Variables and Data types on C++


Thanks to variables we can store data in the program. But so far, we haven't done any surgery on him.


It's free to keep the data, but it's not processed.


Well, to process it we need an operator.


What is Operator?


and how to use it?


Let's discuss…


What is Operator?


The operator is a symbol…


Symbols used to perform a specific operation.


For example:


We want to sum the values of the variables x and y, then we can use the addition operator (+).


x + y's


There are six types of carrier groups in C++ programming that you should know:


Artimatic Operator;


Assignment Operator;


Comparator Operator;


Logic Operator;


Bitwise Operator;


and Other Operators.


What are the differences between all these types of operators?


Let's discuss, one by one…



Arithmetic Operator



An arithmetic operator is an operator used to perform arithmetic operations.


This operator consists of:


Operator NameSymbolAddition+Reduction-Multiplication*Partition/Save For%


How to wear it?


Let's try in the example program:


Create a new program called operator_aritmatika.cpp, then fill it with the following code.


#include using namespace std; int main(){ int a, b, c; cout << "Splink value a: "; cin >> a; cout << "Follow value b: "; cin >> b; // using addition operator c \= a + b; cout << "Result a + b \= " << c << endl; return 0; }


The result: then:



Try also for examples of other operators such as subtraction, multiplication, division, and remainder for.


Turn the program into this:


#include using namespace std; int main(){ int a, b; cout << "Input value a: "; cin >> a; cout << "Inclose value b: "; cin >> b; cout << "Result a + b: " << a + b << endl; cout << "Result a - b: " << a - b << endl; cout << "Result a *b: " << a *b << endl; cout << "Result a /b: " << a / b << endl; cout << "Result a %b: " << a %b << endl; return 0; }


The result: then:



In a division operation, 7/2 results in 3.


Why so?


It shouldn't be 3.5…


This is because we perform operations against integer data types.


When we change the program it uses float data type:


#include using namespace std; int main(){ float a, b; cout << "Splink value a: "; cin >> a; cout << "Follow value b: "; cin >> b; cout << "Result a /b: " << a /b << endl; return 0; }


The result will be like this:




Assignment Operator



Assignment operator (Assignment Operator) is an operator to assign tasks to variables. Usually to fill in the value.


Assignment Operators consist of:


Operator NameSymptoms Charging Values\=Filling and Addition+\=Filling and Reduction-\=Filling and Multiplication*\=Filling and Sharing/\=Filling and Remaining for%\=Filling and shift left<<\=Filling and shift right>>\=Filling and bitwise AND&\=Filling and bitwise OR|\=Filling and bitwise XOR^\=


Let's try it in the program…


Create a new program called operator_assignment.cpp, then fill it with the following code:


#include using namespace std; int main(){ int a, b; // filling value with operator \= a \= 5; b \= 10; // charging at once addition of b +\= a; // it is same as b \= b + a cout << "Result b +\= a is " << b << endl; // charging at once reduction b -\= a; // it is same as b \= b - a cout << "The result b -\= a is "<< b << endl; // charging at once multiplication b *\= a; // this is the same as b \= b * a cout << "Result b *\= a is " << b << endl; // charging at once division b /\= a; // it is the same as b \= b / a cout << "Result b /\= a is " << b << endl; // charging at the same time addition b %\= a; // it's same as b \= b % a cout << "Result b %\= a is " << b << endl; return 0; }


Result:



In the program, variable b we refill with the assignment operator.


For example, the operation:


b +\= a


Just like the operation:


b \= b + a


This means that we will fill the value for b with the value of b plus the value of a.


Same with other opeartors.


b *\= a; // -> b \= b * a


Fill b with the value of the multiplication b with a.


The point is, what you need to remember:


“assignment operator used to fill value.”



Comparator Operator



The comparison operator is the operator to assign two values. This operator is also known as a relation operator.


Comparator operators consist of:


Operator NameSymbolMore>More Small\=More Small Same as<\=


The values resulting from the comparison operation will be true and false.


In C++, true values will be equal to 1 and false will be equal to 0.


Let's try it in the program…


Create a new program with the name operator_comparator.cpp, then fill it with the following code:


#include using namespace std; int main(){ int a \= 4, b \= 3; bool results; cout << "a \= " << a << endl; cout << "b \= " << b << endl; // using comparison operator results \= a > b; cout << "a > b \=" << results << endl; results \= a < b; cout << "a < b \= " << result << endl; result \= a >\= b; cout << "a >\= b \= " << result << endl; result \= a <\= b; cout << "a <\= b \= " << result << endl; result \= a \=\= b; cout << "a \=\= b \= " << result << endl; result \= a !\= b; cout << "a !\= b \= " << result << endl; return 0; }


The result: then:



Comparison operators will usually be used when making branching.



Logic Operator



If you have ever learned mathematical logic, you will certainly not be familiar with this operator.


Operator NameSymbolLogic AND&&Logic OR||Negasi/reverse!


Logic operators are used to create logical operations.


Like this one: for example:


Statement 1: Farmer Code a programmer


Pernyattan 2: Petanikode uses Linux


If asked, is Farmer Code a programmer who uses Linux?


Of course we'll check the truth first


Statement 1: Farmer Code a programmer \= true.


Statement 2: Petanikode using Linux \= true.


What is the programming code and uses Linux?


Statement 1 && Statement 2 \= true


Confused?


Try checking the truth table again for AND logic.


Pain 1Pernytaan 2Logic Results ANDtruetruetruetruefalsefalsetruefalsefalsefalsefalse


Still confused…?


Looks like you have to open again math logic lesson 😄.


How are the users in the program?


Let's look at an example…


Create a new program called operator_logika.cpp, then this with the following code.


#include using namespace std; int main(){ int a \= 1; // true int b \= 0; // false bool results; cout << "a \= " << a << endl; cout << "b \= " << b << endl; // logic AND results \= a && b; cout << "a && b \= " << result << endl; // logic OR result \= a || b; cout << "a || b \=" << result << endl; // logic NOT cout << "!a \= " << !a << endl; return 0; }


Result:




Bitwise Operator



A bitwise operator is an operator used for operations based on the bit (binary) of a value.


Bitwise operators consist of:


Operator NameSymbol in JavaAND&OR|XOR^NOT/complement~Left Shift<>


For those of you who have studied number systems and digital systems will easily understand how this operator works.


But for those of you who haven't…


I will try to explain it in an easy way.


All righty!


Suppose we have 6 and 3.


The values 6 and 3 are then converted into binary numbers.


It will be like this:


6 \= 0110 3 \= 0011


Well, bitwise operators will perform operations based on those binaries.


Let's look at the first example:


Bitwise AND (&)


Bitwise AND is a bit operation based on AND logic, take note of this image.



Note the binary number for values 6 and 3.


If we do an AND operation there, it will generate a new binary number.


Then the resulting binary is converted back into decimal numbers.


The result is 2.


Let's try it in the program.


Create a new program called bitwise_and.c, and then fill it with the following code:


#include using namespace std; int main(){ int a \= 6; int b \= 3; int results; // using operator bitwise and results \= a & b; cout << "a & b \= " << result << endl; return 0; }


The result: then:



Bitwise OR (|)


The bitwise OR operator is also the same as bitwise AND.


The bitwise OR operator will generate a false value or 0 when both are false.



Program Example: bitwise_or.c


#include using namespace std; int main(){ int a \= 6; int b \= 3; int results; // using operator bitwise or results \= a | b; cout << "a & b \= " << result << endl; return 0; }


The output:


a | b \= 7


Bitwise XOR (^)


The XOR operator (Exclusive OR) will produce a value of 1 when the two values are not equal.



Program Example: bitwise_xor.c


#include using namespace std; int main(){ int a \= 6; int b \= 3; int result; // using operator bitwise xor result \= a ^ b; cout << "a ^b \= " << result << endl; return 0; }


The output:


a ^ b \= 5


Bitwise NOT (~)


Bitwise NOT is also known as complement.


This operator will generate an inverse binary value from the original binary.


Then represented by two components.



Program example: bitwise_not.c


#include using namespace std; int main(){ int a \= 6; int results; // using operator bitwise not results \= ~a; cout << "~a \= " << result << endl; return 0; }


The output:


~a \= -7


Bitwise Left Shift (<<)


The bitwise left shift operator will generate a binary value that is shifted to the left.


Example:


6 << 1


Then the binary niai of 6 will be shifted as much as 1 bit to the left.


Example: left_shift.c


#includes


using namespace std;


int main(){


int a \= 6;


int results;


// using bitwise left shift operators


result \= a << 1;


cout << "a << 1 \= " << result << endl;


return 0;


}


Result:


a << 1 \= 12


Bitwise Right Shift (>>)


Bitwise right shift is the same as left shift. The difference lies in its direction.


Right shift will shift the bit to the right.


Example:


6 >> 1


Then the binary value of 6 will be shifted to the right by 1 bit.



Example: right_shift.c


#includes


using namespace std;


int main(){


int a \= 6;


int results;


// using bitwise left shift operators


result \= a \>\> 1;


cout << "a >> 1 \= " << result << endl;


return 0;


}


Result:


a >> 1 \= 3



Miscellaneous Operators



Aside from the operators we discussed above, there are several other operators to know:


Operator Name Symbol Remarks


Address &. to take 


memory address.


Pointers. *. to create 


pointers


Ternary. ? : to create conditions


Increments. ++. to add 1


Decrement - to subtract 1


Operator & if used on one variable, will function to retrieve the memory address of the variable.


And the operator* serves to need a pointer.


Example:


#includes


using namespace std;


int main(){


int a \= 5;


int *ptr_a \= &a;


cout << "Variable memory address a is "<< &a << endl;


// change the value of a from pointer


cout << "Value a start \=" << a << endl;



\*ptr\_a \= 21;


cout << "Value a modified \=" << a << endl;


return 0;


}


Result:



Next there is a ternary operator to create the condition. How it works like if/else branching.



Examples


#includes


using namespace std;


int main(){


int a \= 4;


// using a ternary operator


result string \= a > 1 ? "right": "wrong";


cout << "a > 1 is " << result << endl;


return 0;


}


The output:


a > 1 is true


Finally, there are increment and decrement operators to add and subtract values by 1.


Example:


#includes


using namespace std;


int main(){


int a \= 4;


// increment of a


a\+\+;


cout << "a\+\+ \= " << a << endl;


// increment again a


++a;


// decrement of a


a-;


cout << "a--\= " << a << endl;


// decrement again a


--a;


cout << "--a \= " << a << endl;


return 0;


}


Hasil outputnya:


a++ \= 5


++a \= 6


a-\= 5


- a \= 4


Increment and decrement operators can be placed in front of or behind variables or values.


This operator is usually used on looping blocks.


What Next?


We've learned the same type of operator in the C programming language…


These operators will often be used in making programs.


So make sure you understand it. If you do not understand, you can ask through comments.


Next please learn about:


Learn C++ #07: Branching Blocks