From the previous lesson we know of the existence of variables and constants, we can begin to operate with them. For that purpose,  C++ integrates operators. Unlike other languages whose operators are mainly keywords, operators in C++ are  mostly made of signs that are not part of the alphabet but are available in all keyboards. 

You do not have to memorize all the content of this page. Most details are only provided to serve as a later 
reference in case you need it.  

Assignment (=)  Operator

The assignment operator assigns a value to a variable, such as
       a = 5; 

This statement assigns the integer value 5 to the variable a. The part at the left of the assignment operator (=) is 
known as the lvalue (left value) and the right one as the rvalue (right value). The lvalue has to be a variable 
whereas the rvalue can be either a constant, a variable, the result of an operation or any combination of these. 


               a = b; 
This statement assigns to variable a (the lvalue) the value contained in variable b (the rvalue). The value that was  stored until this moment in a is not considered at all in this operation, and in fact that value is lost.  

Consider also that we are only assigning the value of b to a at the moment of the assignment operation. Therefore  a later change of b will not affect the new value of a. For example, let us have a look at the following code -                                            a:4 b:7 
using namespace std; 
int main () 

  int a, b;         // declaration of integer variable
  a = 10;           // a:10, b:? 
  b = 4;            // a:10, b:4 
  a = b;            // a:4,  b:4 
  b = 7;            // a:4,  b:7 
  cout << " a:" << a <<endl;   
  cout << " b:" << b <<endl; 
  return 0; 

The output is:   a : 4
          b : 7

This code will give us as result that the value contained in a is 4 and the one contained in b is 7.

Arithmetic operators ( +, -, *, /, % ) 

The five arithmetical operations supported by the C++ language are:  

+   ,   addition 
-    ,   subtraction 
*    ,  multiplication 
/    ,  division 
%   ,   modular division 

Operations of addition,  subtraction,  multiplication and  division  literally correspond with their respective 
mathematical operators. The only one that you might not be so used to see is modulo; whose operator is the 
percentage sign (%). Modulo is the operation that gives the remainder of a division of two values. For example, if  we write:  

    a = 11 % 3; 

the variable a will contain the value  2 , since 2 is the remainder from dividing 11 between 3.  

Compound assignment (+=, -=, *=, /=, %=,  &=,  ^=, |=) 

When we want to modify the value of a variable by performing an operation on the value currently stored in that  variable we can use compound assignment operators:  

 expression                         is equivalent to 
value + = increase;            value = value + increase; 
a = 5;                               a = a  5; 
a /= b;                              a = a / b; 
price *= units + 1;            price = price * (units + 1); 

and the same for all other operators. For example:  

using namespace std; 
int main () 

  int a , b=3; 
  a = b; 
  a+=2;             // equivalent to a=a+2
  cout << " a " << a <<endl; 
  return 0; 

The output is:   a = 5

Increment and decrement( ++,  -- ) 

Shortening even more some expressions, the increase operator (++) and the decrease operator () increase or  reduce by one the value stored in a variable. They are equivalent to  +=1 and to =1, respectively. Thus:   


are all equivalent in its functionality: the three of them increase by one the value of c.  

Logical operators ( !, &&, || ) 

The Operator   is the C++ operator to perform the Boolean operation NOT, it has only one operand, located at its  right, and the only thing that it does is to inverse the value of it, producing false if its operand is true and true if its  operand is false. Basically, it returns the opposite Boolean value of evaluating its operand. For example:  

    ! (5 == 5)    // evaluates to false because the expression at its right (5 == 5) is true. 
    ! (6 <= 4)    // evaluates to true because (6 <= 4) would be false.  
     true        // evaluates to false 
     false       // evaluates to true.   

The logical operators  &&  and   ||  are used when evaluating two expressions to obtain a single relational result. The  operator && corresponds with Boolean logical operation AND. This operation results true if both its two operands  are true, and false otherwise. The following panel shows the result of operator && evaluating the expression a &&  b:  

  a          b        a && b                 a | | b
true      true        true                    true
true      false       false                   true
false     true        false                   true
false     false       false                   false

The operator  || corresponds with Boolean logical operation OR. This operation results true if either one of its two  operands is true, thus being false only when both operands are false themselves.

In terms of  Hexadecimal conversion Bitwise operator can be written as

bit 1   bit 2    OR (||) AND ( & ) XOR ( ^)
0 0 0 0 0
1 0 1 0 1
0 1 1 0 1
1 1 1 1 0


No comments:

Post a Comment