Beginning C++: Operators

Home / Beginning C++: Operators

Beginning C++: Operators

December 3, 2015 | Article | No Comments

When writing a program we are often demanded to define operations, either calculations, comparison, etc. To do it we need operators and C++ has a complete arsenal that can be used. In this article we will divide our focus into some subjects: assignment operator, unary operators, binary operators, ternary operators.

Assignment Operator

Assignment is a operator for storing a value into a variable or a constant. This operator is symbolized by equal sign (=). Let’s take a look of it in this snippet:

#include <iostream>
using namespace std;

int main() {
	// when declaring a constant we have to assign a value immediately.
	const float PHI = 3.14;

	// declaring variables
	char myChar;
	int myInt;
	double myDouble;

	// assign each variable
	myChar = 'A';
	myInt = 135;
	myDouble = 21.0378;

	// print out the value of each variable
	cout<<myChar<<endl;
	cout<<myInt<<endl;
	cout<<myDouble<<endl;
	return 0;
}

Unary Operator

An unary operator is an operator which only has one operand for the operation. These type of operator has following operators:

Operator Operations Example
+ Make a number become positive +7
Make a number become negative -7
++ Increment C++
Decrement C–

An increment is an incremental of a value on a variable by one. For example a variable C initially has value 4 and we do incrementing it then the value of C now become 5. Similar with increment, a decrement take a variable and do decremental of a value by one. Thus, when C is initially has value 4 and we do decrementing then the value of C will become 3.

There are to type of increment and decrement: pre and post. Both will have same result in the end but they are different in operation.

The pre- (either increment or decrement) would do value-changing first before being used. While in post- (either increment or decrement) the value is changed after being used.

Let’s take a look of how the concept mean:

#include <iostream>
using namespace std;

int main() {
	int x = 5;
	int y = 5;

	cout<	cout<	cout<
	x = 5;

	cout<	cout<	cout<
	cout<	cout<	cout<
	y = 5;

	cout<	cout<	cout<
	return 0;
}

Binary Operator

In mathematics, a binary operator is an operator which has two operand for its operation. In C++, we divide binary operators into smaller categories: arithmetic operators, logic operators, relational operators, and bitwise operators.

Arithmetic Operator

Arithmetic operators are operators used for computing and calculating a value from another value. Like what we have learned from school, we know there are some operators in mathematics and we have it too on C++:

Operator Operations Example
+ Addition 2 + 7 = 9
Reduction 3 – 1 = 2
* Multiplication 3 * 5 = 15
/ Division 6 / 3 = 2
% Modulo 11 % 3 = 2

You must be familiar with +, -, *, and / operator. But please note for / operator, this operator will result on integer value if both of operands are integers. So if you have a = 5 and b = 2 and do a / b, instead of 2.5 you will get 2. To get a real value, you must supply one of operand (either numerator or denominator) with real-valued number.

Some of you maybe aren’t familiar with modulo operation. In simple way, a modulo operation will give an integer for remainder of division. Let’s take example from the table. We have two number, 11 and 3. When we divide 11 with 3, resulting in 3 and has remainder 2 (number that can’t be divided by 3). The modulo operation will give value 2 as a result. Another example: 6 % 4 will give 2, 19 % 5 will give 4, etc.

Logical Operators

Logical operator is an operator used for operation in which resulting only two distinct value: true or false. It is corresponded with boolean value in previous discussion. There are three operators on this category:

Operator Operations Example
&& AND true && true = true
|| OR true || false = true
! NOT NOT false = true
&& (AND) Operator

AND operation will give true only if both of operands are true. Either from that, this operator will give false. Let’s look at this truth table:

X Y X && Y
1 1 1
1 0 0
0 1 0
0 0 0

Remember that false is represented by zero and true is represented by not-zero value.

|| (OR) Operator

OR operation will give result true if one of operands has true value. Let’s see the truth table below:

X Y X || Y
1 1 1
1 0 1
0 1 1
0 0 0
! (NOT) Operator

Value of NOT operation is an inverse of the operand. Well, actually this operator is an unary operator, but for the sake of simplicity I group it with the rest of logical operators. Let’s look at the truth table:

X ! X
1 0
0 1
Relational Operators

A relational operator is an operation to determine relation between two operands. These operators are simply comparison between two operands.

Operator Operations Example
< Less than (5 < 2) = 0
> Greater than (5 > 2) = 1
<= Less than or equal (3 <= 2) = 0
>= Greater than or equal (3 >= 2) = 1
== Equal (3 == 3) = 1
!= Not equal (3 != 3) = 0

Please note that equal operator is consists of two = symbol.

Bit-wise Operators

Unlikely other programming language, C++ support bit-wise operations. A bit-wise operations used for operation of bit manipulation. Let’s take a look for the this table.

Operator Operations Example
& AND 1 & 0 = 0
| OR 1 | 0 = 1
^ Exclusive-OR (XOR) 1 ^ 1 = 0
~ NOT ~1 = 0
<< Left Shift 5 << 1 = 10
>> Right Shift 10 >> 1 = 5

At glance you might think that these operations are similar to logical operators. Well, that’s true in some way. In bit-wise operation, the operand are every bit on both side while the logical operator use whole number as operand and both operand act only as two distinct value true or false.

Let’s take example for counterparts AND: Suppose we have two number, 5 and 2. In logical operator, both 5 and 2 are treated as true this 5 && 2 will give result true. While in bit-wise operation, we treat 5 and 2 as bits which we have 5 = 00000101 and 2 = 00000010. Then, bit-wise AND would result on:

5 00000101
2 00000010
———— &
5 & 2 00000000

Now, as you can see. We do AND operation for each bit and got result 0. With similar operation we will get 2 when we operating 6 and 2 (guess why?).

An exclusive-OR operation is bit-wise operator which similar to bit-wise OR operator. But it only gives 1 if only one operand is 1 (and other is 0). Otherwise it will give 0.

The interesting part is shifting. Shift operation, like the name, is an operation to shift the bit to a direction, The number that would be shifted is the left operand and it will be shift to x place as given by right operand. Let’s look example for left and right shift.

A left shift (a << b) will shift a to b position in left. Suppose we have two number, 5 = 00000101 and 2 and did left shift like this: 5 << 2, the bits of 5 would be shift in a way that result in 00010100 = 20. Note that the result bit is actually shifted 2 position from previous one.

A right shift (a >> b) will shift a to b position in left. Suppose we have two number, 4 = 00000100 and 2 and did the right shift like this: 4 >> 2, the bits of 4 would be shift in a way that result in 00000001 = 1. Note that the result bit is actually shift 2 position form previous one.

Ternary Operator

Ternary operator is an operator which has 3 operands for operation. It seems odd but the operator consists of 2 symbols ? and :. General scheme for this operator is given below:

expression1 ? expression2 : expression3;

This operator consist of 2 stage: checking and executing. First it will check the condition on expression1. If expression1 result in true, the expression2 will be executed. If not, the operator will execute expression3. Now let’s take a look of this snippet:

#include <iostream>
using namespace std;

int main() {
	// declare 2 variables, x with random-initialized value and y with initial value 5
	int x, y = 5;

	// check if y > 0. It would be true so we have -y as a result of this operation
	x = (y > 0) ? -y : y;

	// Let's make sure the result is same as our prediction
	cout<< x <<endl;
}

 

,

About Author

about author

xathrya

A man who is obsessed to low level technology.

Leave a Reply

Your email address will not be published. Required fields are marked *

Social media & sharing icons powered by UltimatelySocial