Bitwise, Precedence and Special Operator – Object Oriented Programming (OOP)

Download our Android App from Google Play Store and start reading Reference Notes Offline.

bitwise, precedence and special operatorBitwise, Precedence and Special Operator
Object Oriented Programming (OOP)
Third Semester | Second year
BSc.CSIT | Tribhuvan University (TU)

Bitwise Operator
In C++ programming language, bitwise operators are used to modify the bits of the binary pattern of the variables. Table below gives use of some bitwise operators:

Table: Use of Bitwise Operator

Operator Asm equivalent Description
& AND Bitwise AND
| OR Bitwise Inclusive OR
^ XOR Bitwise Exclusive OR
~ NOT Unary complement (bit inversion)
<< SHL Shift Left
>> SHR Shift Right

Precedence of Operators
In case of several operators in an expression, we may have some doubt about which operand is evaluated first and which later. For example, let us take following expression:
a = 5 + 7 % 2
Here we may doubt if it really means:
a = 5 + (7 % 2)             // with a result of 6
a = (5 + 7) % 2            // with a result of 0
The correct answer is the first of the two expressions, with a result of 6. Precedence order of some operations in C++ programming language is given in the table below;

Table: Precedence of operators in descending order

Level of Precedence Operator Description Grouping
1 :: scope Left to right
2 () [] . -> ++ — dynamic_cast static_cast reinterpret_cast const_cast typeid postfix Left to right
3 ++   —   ~   ! size of new delete unary (prefix) Right to left
*   & indirection and reference (pointers)
+   – unary sign operator
4 (type) type casting Right to left
5 . * ->* pointer to member Left to right
6 * / % multiplicative Left to right
7 + – additive Left to right
8 <<    >> shift Left to right
9 <  >  <=   >= relational Left to right
10 ==   != equality Left to right
11 & bitwise AND Left to right
12 ^ bitwise XOR Left to right
13 | bitwise OR Left to right
14 && logical AND Left to right
15 || logical OR Left to right
16 ?: conditional Right to left
17 =   *=   /=   %=   +=  _=  >>=    <<=    &=   ^=    |= assignment Right to left
18 , comma Left to right

Grouping defines the precedence order in which operators are evaluated in the case that there are several operators of the same level in an expression. Thus if you want to write complicated expressions and you are not completely sure of the precedence levels, always include parentheses. It will also make your code easier to read.

Special Operators
Apart from the above operators that we have discussed above so far, C++ programming languages supports some special operators. Some of them are: increment and decrement operator, size of operator, comma operator etc.

  • Increment and Decrement Operator
    In C++ programming language, Increment and decrement operators can be used in two ways: they may either precede or follow the operand. The prefix version before the operand and postfix version comes after the operand. The two versions have the same effect on the operand, but they differ when they are applied in an expression. The prefix increment operator follows “change then use” rule and post fix operator follows “use then change” rule.
  • The size of operator
    We know that different types of variables, constant, etc require different amount of memory to store them. The size of operator can be used to find how many bytes are required for an object to store in memory. For example:
    sizeof(char) returns 1
    sizeof(int) returns 2
    sizeof(float) returns 4
    If k is integer variable, the sizeof(k) returns 2.The sizeof operator determines the amount of memory required for an object at compile time rather than at a run time.
  • The comma operator
    The comma operator gives left to right evaluation of expressions. It enables to put more than one expression separated by comma on a single line. For example:
    int i = 20, j = 25;
    In the above statements, comma is used as a operator between the two statements.
(Visited 117 times, 1 visits today)

Posted By : Digvijay | Comment RSS | Category : Third Semester
Tag :

Post a Comment

Your email is never published nor shared. Required fields are marked *


Wordpress DMCA
Community | Toolbar | Android App | Founder/Developer : Hari Prasad Chaudhary | CSIT Portal Manager : Digvijay Chaudhary