# Beginner's Tutorial on Operators

Tutorial On Operators

Operators are the signs to the tell the compiler to perform specific task. The Operators fall into following categories :-

``````Arithmatic Operator
Relational Operator
Logical Operator
Bitwise Operator
Miscellaneous Operator(i added Assignment Operators in this categeory)
``````

Arithmatic operators:-
There are following arithmetic operators:-

``````+       Add                     [6+2=8]
-       Subtract                [6-2=4]
*       Multiply                [6*2=12]
/       Divide                  [6/2=3]
%       Modulus(Reminder)       [7%2=1]
``````

Relational Operator :-
Relational Operators are used to compare 2 values and result in true [1] or false [0].

``````<        less than
>       greater than
<=       less than equal to
>=      greater than equal to
==      equal to
!=      Not equal to
``````

Lets take an example:-
`A=5 and B=10`

``````            CONDITION       RESULT
A<B              TRUE
A>B             FALSE
A<=B         TRUE
A>=B            FALSE
A==B            FALSE
A!=B            TRUE
``````

Logical Operators:-

Logical operator are used to combine more than one expression .

``````&&      =       LOGICAL AND
||      =       LOGICAL OR
!       =       LOGICAL NOT
``````

&& and || are binary operator whereas ! is unary operator. && and || are called binary because both work on 2 operand where ! works on single operand
&& Operator Produce 1[true] if the all expressions are true. And it produce 0[false] if any one expression is false. let’s see it truth table .

``````EXP1        EXP2        RESULT
TRUE        TRUE        TRUE
TRUE        FALSE       FALSE
FALSE       TRUE        FALSE
FALSE       FALSE       FALSE
``````

Lets see the example below(`a=10,b=20,c=30`)

``````Expression               Expression evaluation      Result
a<b      &&     a<c           true    &&  true        True
a<b     &&     a>c           true    &&  false       False
``````

|| Operator produce 1[true] if either any one expression or all the expressions are true. And it produce 0[false] if both expression are false. Let’s see its truth table.

``````EXP1        EXP2        RESULT
TRUE        TRUE        TRUE
TRUE        FALSE       TRUE
FALSE       TRUE        TRUE
FALSE       FALSE       FALSE
``````

Lets see the example below(`a=10,b=20,c=30`)

``````Expression              Expression evaluation       Result
a<b      ||   a<c     true    ||  true            true
a<b      ||  a>c         true    ||  false           true
a==b    ||  a>c         false   ||  false           false
``````

! Operator:- if the result is true then it convert it to false and if the result is false then it convert it to true. Any number except 0 is true and 0 itself for false. Lets see its truth table

``````Exp     Result
TRUE        FALSE
FALSE       TRUE
``````

Lets see the example below(`a=10,b=20`)

``````Expression      Expression Evaluation       Result
!(a<b)           !(tue)                      false
``````

Bitwise Operators

``````&       Bitwise And
|       Bitwise OR
^       Bitwise Exlusive OR(XOR)
~       Bitwise one’s complement
<<        Bitwise Left Shift
>>      Bitwise Right Shift.
``````

The & operator performs bitwise AND on 2 integers . Each bit in the result is 1 when both corresponding bits are 1.
Lets see the example below(`a=23 , b=11`)

``````a = 23 or 0001 0111
b= 11 or 0000 1011
a&b   =  0000 0011  [which is equivalent to 3]
``````

The | operator performs bitwise OR on 2 integers . Each bit in the result is 1 when any one bit or both bits are 1 in the inputs.

Lets see the example below(`a=23 , b=11`)

``````a = 23 or  0001 0111
b= 11  or  0000 1011
a|b    =   0001 1111    [which is equivalent to 31]
``````

The ^ Operator performs bitwise XOR which produce 1 in each bit if only one , but not both of the corresponding bits in the input is 1.
Lets see the example below(`a=23 , b=11`)

``````a =23 or 0001 0111
b =11 or 0000 1011
a^b   =  0001 1100  [which is equivalent to 28]
``````

The ~ Operator performs on single operand (that’s why it is a unary operator ) And it return bitwise one’s complement of its oerand. That is, every bit that is 1 in the operand is 0 in the result. Conversely, every bit that is 0 in the operand is 1 in the result. The operand to the one's complement operator must be an integral type.
Lets see the example:-

``````            0000  0000  0000  1011 (16 bits representation of integer 11)
So, ~11 is  1111  1111  1111  0100
``````

The << Operator shifts its first operand left by a number of bits given by its second operand, filling in new 0 bits at the right.
Lets see the example:-

``````2<<3  [left operand is 2(0000 0000 0000 0010) and right operand is 3]
The result will be = 0000  0000 0001 0000 [which is equivalent to 16].
``````

The >> Operator shift the first operand right by a number of bits given by its second operand.

``````16>>2   [left operand is 16(0000  0000  0001  0000) and right operand is 2]
The result will be = 0000  0000  0000 0100.[which is equivalent to 4].
``````

Miscellaneous Operator:-

There are also operators that don’t fall into any of the above category . That’s why i put these all remaining operators in category named “Miscellaneous Operators” . And these are:-
Just consider `a=10 and b=2`

``````Operator        purpose                             Example
=           for the purpose of assignment           a=b (initialise value of b to a)
+=          a+=b is similar to a=a+b                a+=b results in 12
-=          a-=b is similar to a=a-b                a-=b results in 8
*=          a*=b is similar to a=a*b                a*=b results in 20
/=          a/=b is similar to a=a/b                a/=b results in 5
Sizeof      Return the size in bytes of the operand.sizeof(int)
++          increase the value  by 1                ++a result in 11
--          decrease the value by 1                 --b results in 1
``````

Note : ++ and -- are unary operator , its mean that these operators work with one operand . and they can be pre and post .

``````Pre                         post
++a                         a++
--b                         b--
``````

sometimes people think that both are same but in fact both are different. lets see Example:-

``````int a,b;
a=10;
b=a++;                          b=++a;
``````

`b=a++` will first initialise `b` to the value of `a` then `a` gets incremented.
`b=++a` will first increment the value of `a` and then initialise it to `b`.
`b=a++` Here `b` will be 10 and `a` will be 11
`b=++a` Here `b` will be 11 and `a` will also be 11

Its all about what i’ve got in the chapter titled operator . I see some other operators also but books and some other resources say that these will be discussed later. So whenever i read these remaining operator , i will definitely add these operators to this tutorial. As i beginner and this tutorial is completely made with my own efforts and there may be some mistake , if anybody findout any mistake in the tutorial then it will a good opportunity for me to learn something new.

Here i would like to give special thanks to the person here with user name deceptikon who helped me a lot for making concept clear And he also encourage me to upload my own tutorial here.

ddanbe commented: Nice! +14
Gà_1 0

I have an integer variations a, if:
a/10 then what is the result?

`/` operator divide numerator by denominator.

``````    4/2=2
5/2=2 (due to both operands are integral type)
``````

you can also get exact result by using type casting:-

``````    (float)5/2=2.5
(float)a/2=depends on the value of `a`
``````

so , don't forget to initialise the variable `a`. if it is of type `auto` then it will contain garbage value(if you don't initialise).

i think that now you don't have problem with this issue but if you have some more problems on it, then don't be hesitate to create new thread with appropriate title and ask you problem there(I am also a c++ Beginner).

Gà_1 0

Thanks you, that mean when a<10, a/10 will equal to 0?

that mean when a<10, a/10 will equal to 0?

yes , if you don't use any type casting then it will give result 0.

MandrewP 60

b=a++ will first initialise value of a to b then a get incremented should actually read:

b=a++ will first initialise b to the value of a then a gets incremented.

ok.
thats may be due to language problem as my english is not so great.

i will contact to a moderator to modify it.

NathanOliver 429

Nice little tutorial.

such nice responses on this little tutorial encourage me to learn more c++ in quick way and write more tutorials on it.I will continue to strive for nothing short of the best.

thanks.

nice tutorial for beginners

I was totally unaware about associativity until few days ago(maybe i accidently skipped this section while reading about operators).Anyways , i get this concept while having conversation with mike.Then I decided to add this information to the tutorial But They(Admins) don't agree to change the content citing some policy related reasons.Therefore i add this little information here.

Expression

Expression is made up of variables , constants and operators. Look below(`B=10`):-

`A=B+2*3`

Here `A` and `B` are two variables and 2, 3 are integer constants. `+` and `*` are two of arithmetic operators. Hence it’s an example of Expression. Now the question arise that what operators will be evaluated first. This concept is called operator precedence. Operator precedence tells that what operator will be executed first(i.e it tells their priority ).

For a while , I tell you that`* , / , %`have equal priority and has higher precedence than `+` and `-` . So the solution of the above expression is as follow:

`A=B+(2*3)` Because `*` has higher priority than `+`
therefore `*` will be evaluated first and then `+` will be evaluated

``````A=B+6
A=16(A=10+6).
``````

Now assume you face such an expression which consists of operators which have same precedence. Look below (`I=20,J=2`)

`K=I/J*J%3`

How would you solve this expression? In such case where operators with same precedence exist then we have to solve the expression by keeping the associativity rules in mind. Associativity means that how operator will be executed ?(right to left or left to right).for a while , I tell you that`/,*`has left to right associativity . it means that the expression will be evaluating from left to right , as follow :-

``````K=(((I/J)*J)%3);
K=(((20/2)*2)%3)
K=((10*2)%3)
K=(20%3)
k=2
``````

I desined an image which depict the precedence and associativity.But i can't attach the image here.So , you need to visit this link.Operators are listed there in Precedence order.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.