C++ operators:Logical operators and Bitwise operators

C++ operators,logical operators and bit wise operators

C++ operators are usually some symbols-there are non-symbols operators also- used to perform various operations in our program.Operations like comparing two expression or changing the bits pattern of the data can be performed using operators.Although C++ operators can be classified into many types, but here we will discuss the two operators given below:

i)Logical operator and
ii)Bit wise operator.

There are other operators like mathematical operators,assignment operators,relational operators,etc.these operators are discussed in another post.

Linl:More operators: Mathematical , assignment , relational , Unary , typeid , ternary,etc

Logical operator

Logical operators perform operation using the true and false value of an operand or an expression.When logical operator is used the operands are assumed to take the value true or false.One way of knowing which operand will have true value and which operand is false when using integer or float or char as operand is,all the non-zero value -it can be either negative or positive value- is interpreted as true and zero as false.

Operand -True or false
Fig. Operand type and it’s true or false value.

If an expression is used as an operand the true or false value can be deduced from the expression itself,you will see later how? when discussing the various types of logical operator.Since the resultant value we get is always either true or false when logical operator is evaluated,an expression using logical operator is use for making decision in our program(more detail about this in Chapter 2).In C++ there are three logical operators,each one of them is discuss in detail below.

i)AND denoted by ‘&&‘.
ii)OR denoted by ‘||‘ and
iii)Negation denoted by ‘!‘.

i)AND or && :&& will give a resultant value as true only when both the operands or expressions are true otherwise false.Look at the code below.

char c1=’9′ , c2=’Z’ ;
int i1=9 , i2=100 ;
int result= (c1 < i1) && (i2 < c2) ;
cout<< result << endl ;

cout<< (2 && 78) << endl  /// Gives true
    << (-23 && 0 ) << endl  ///Gives false
    << (90.789 && -78.5 ) ;  ///Gives true

In the expression (c1 < i1): c1(9) is not smaller than i1(9) so ,the expression (c1 < i1) evaluates to false .We can now predict that the expression (c1 < i1) && (i2 < c2) or the value of result will be false since the first expression value is false and ‘&&‘ requires that both the operands must be true for the expression to evaluate as true.

i)OR or || :This operator will give the resultant value of an expression as false only when both the operands are false.An example code is given below.

char c1=’&’ , c2=’?’ ;
int i=9;
int result=(c1 > c2) || i ;
cout<< result ;

cout<< (23 || 0 ) << endl  ///Gives true
    << ( -34.56 || 0 ) ;  ///Gives true

In the expression “(c1 > c2) || i“, the expression (c1 > c2) is true because c1(‘&’) value is 38 (from ASCII chart) and c2(‘?’) value is 63(from ASCII chart) and as for i(9) the value is true because the compiler consider every integer value not equal to 0 as true.Hence the value of result is 1.

i)Negation or ! :Negation always give the opposite value of what it is.For instance if the expression is true negating it will give false as the resultant value.

float f=2.356 ;
int i=2 ;
char c=’0′;
int result1=!(f>i) , result2=!c ;
cout<< result1 << “,” << result2 ;

The output is ,

I am guessing you know the reason of the output above.

Bit wise operators

Bit wise operators perform operation at binary level.”At binary level” I mean the raw bits that makes up every data(letters,image,movies,etc.) in our computer .For example consider the letter ‘B’,this letter ASCII value is 66 and 66 in binary format is 1000010.If the letter ‘B’ is utilized as one of the operands in an expression involving bit wise operators then it’s binary format ‘1000010’ is taken to perform the operation not the value 65.Since the operand of a bit wise operator are in binary format there can be only two operands value :1 or 0,using these value an bit wise operators perform the operation.There are six types of bit wise operators in C++,each of them is discuss in detail below:

i)AND denoted by ‘&‘.
ii)OR denoted by ‘|‘.
iii)Exclusive or denoted by ‘^‘.
iv)Left shift denoted by ‘<<‘.
v)Right shift denoted by ‘>>‘ and
vi)Bitwise complement denoted by ‘~‘.

i)AND or &:This operator like the logical ‘and’ operator also gives true only when both the operands are true but for bit wise ‘AND’ instead of true we consider the operand as 1.So AND bit wise operator gives 1 only when both the operand bits are 1 otherwise 0.A program implementing this operator is given below.

int i1=5 , i2=15 ;
int result=i1 & i2 ;
cout<< result ;

The output is


To explain why the value of result is 5,let’s go down to the binary format of i1 and i2 variable and perform the operation at binary level.

Bit wise AND operation
Fig. Bit wise AND operation

The resultant binary digit of the evaluation i1&i2 is 0101 ,since result is integer type the binary digit is converted to integer value which is 5.

ii)OR or |:The bit wise ‘or’ operator gives a resultant value of 0 only when both the operands are 0,if any of the operand value is 1 then the resultant value is 1.

int i1=97, i2=156 ;
int result=97|156 ;
cout<< result ;

The output is 253.
The ‘or’ operation at binary level is shown below.

Fig. Bit wise OR operation

iii)Exclusive or or ^:This operator works more or less like the OR operator but it gives 1 only when one of the operand value is 1.If both the operand value is 1 or 0 then the value evaluates to 0.The bit wise exclusive or performed between the value 57 and 37 is given below.
Exclusive OR operation
Fig. Exclusive OR


Stop wasting time,earn money($$$) from your website-Join Now!

iV)Left shift or << :Left shift operator will shift the left most bit to the left side.How many time the bit is moved depends on the value that append the operator.For instance if the expression is 29 << 3,the left most bit will move left side three times.Considering the size of 29 as 8 bits the shifting of bits is shown below.

Fig. Left shift bit wise operation

V)Right shift or >> :Right shift operator shift the bit right side.The number of times the bit is shift is given by the value append after the operator.

Fig. Right shift operation

If you look at the resultant value after shifting the bits ,you will notice the right shift will always decrease the value of the operand.For instance,29>>3 gives 3 whereas left shifting it gives you 232.The left shifting increases the value however,it is not an absolute rule that Left shifting will always increase the value of the operand.For instance consider the character ‘ê’ whose ASCII value is 136,performing ‘e'<<3 will give ‘P‘ whose integer value is 80;the value is decreased.So Left shifting can either increase or decrease the value ,but right shift will always decrease the value of the operand.

V)Bit wise complement or ~ :This operator exchange the bit of the binary digit.
Bit wise complement
Fig. Bit wise complement

*Side Note

Some points to note:

 i) Shifting the number towards left is same as multiplying the number by 2n,where ‘n’ is the shift count.

unsigned int n=2 ;

cout << (4 << n) << endl ///gives 16
<< 4*pow(2 , n) ; /// same as 4*22 ,and gives 16

Performing multiplication by using the left shift is way faster than the method use in the second code: 4*pow(2 , n).

Link: C++ cmath pow( )

 ii) If we perform right shift the resultant value is same as performing division to the number by 2n,where ‘n’ is the shift count.But this holds true only when 2n exactly divides the number.

unsigned int n=2 ;

cout << (16 >> n) << endl ///gives 4
<< 16/pow(2 , n) ; /// same as 16/22 ,and gives 4

cout << (26 >> n) << endl ///gives 6
<< 26/pow(2 , n) ; /// gives 6.5

“16 >> n” and “16/pow(2 , n)’ gives the same value because 16 is exactly divisible by 22.But 26 is not exactly divisible by 22 ,so “26 >> n” and “26/pow(2 , n)” gives different value.

Related Link

->More operators: Mathematical , assignment , relational,Unary , typeid , ternary,etc.
->More types:Void,bool,decimal,octal,hexadecimal,etc.