Operators in C - Part 3 0 comments

Continued from Part 2

vi) Bitwise Operators : We have already said that, numbers are internally stored as 1's and 0's which are called Bits. Though we may say 8 + 6, internally these numbers are represented in binary and addition is carried out on them. C has a set of operators that allow as to perform the binary operators. These operators comes in handy when we need to speed of certain operations. For example, I need to multiply a number (say 3) by 2. The result of the operation is 6 as we know. In binary 3 is represented as (00000011). Now if we shift the bits by one position towards the left, we get (00000110), which is the equivalent of 6. So inorder to multiply a number by 2, we can left shift the binary equivalent once. If we need to multiply by 4, we have to left shift twice. Let us have a look at the binary operators.

There are various other uses of these bitwise operations like the & operator (Bitwise And) which comes in handy when we need to find out whether any flag is set.

Code
int iNum1 = 5; // Binary Equivalent - 0101

int iNum2 = 11; // Binary Equivalent - 1010

// Bitwise And Operation
printf("%d & %d gives %d\n",iNum1,iNum2,(iNum1&iNum2));

// Bitwise Or Operation
printf("%d | %d gives %d\n",iNum1,iNum2,(iNum1|iNum2));

// Bitwise Exclusive OR
printf("%d ^ %d gives %d\n",iNum1,iNum2,(iNum1^iNum2));

// Left Shift
printf("%d << 1 gives %d\n",iNum1,(iNum1<<1));

// Right Shift
printf("%d >> 1 gives %d",iNum2,(iNum2>>1));

Output
5 & 11 gives 1
5 | 11 gives 15
5 ^ 11 gives 14
5 << 1 gives 10
11 >> 1 gives 5

So what has happened, the Bitwise And operator and Or operators followed the truth table as explained in the Relational Operators in this post. It takes one bit from iNum1 and performs an AND/OR/XOR operation with the corresponding bit in the iNum2 operation.

Lets perform the And operation following the truth table.
iNum1 = 5  = 0 1 0 1
& & & &
iNum2 = 11 = 1 0 1 1
AND gives = 0 0 0 1 = which Equals to 1 (in decimal)

As shown above we perform AND operation on corresponding bits of the numbers.

Similarly XOR follows the truth table
ABOutput
000
011
101
110

Hence (0101) ^ (1011) gives (1110) which is 14 in decimal.

And this XOR operator can be used for SWAP two numbers without using a tempory variable. (Will explain this later).

And I have already said about Left shift and right shift operators. They shift the binary digits one position towards left or right.

vii) Conditional or Ternary Operator : Ok I need to find which is the biggest of two numbers. how will i write the code ?
if a is greater than b then 
biggest number is a
else
biggest number is b

Now that is 4 lines. Why not in a single line ? Yes for that we use Ternary Operator. and This is how we write it
Code
   int iNum1 = 5; 

int iNum2 = 11;

// Ternary Operator - expr1?expr2:expr3
// expr1 is the condition to be evaluated
// expr2 is the statement to be executed when condition expr1 is true
// expr3 is the statement to be executed when condition expr1 is false
int iNum3= (iNum1 >> iNum2)?iNum1:iNum2;

Expr1 (5 >> 11) is false. So Expr3(iNum2) is evaluated. Thus iNum2 is assigned to iNum3.

The drawback is only one statement can be used in Expr2 or Expr3.

viii) Special Operators : There are many special operators like
& - Pointer Operator - Address Of Operator
* - Pointer Operator - Value at Operator
-> - Pointer Operator - Member Selection operator
. - Member Selection operator
, - Comma Operator
sizeof() - SizeOf Operator

Here I will explain comma(,) operator alone. As the others require detailed post and cannot be stuffed in here.

Consider the following code,
Code
   iNum1 = 5;
iNum2 = 10;
iNum3 = iNum1+iNum2;

This above code can be written in single line using the comma operator like this.
Code
      iNum3 = (iNum1 = 5,iNum2 = 10,iNum1+iNum2);

The comma operator is used for linking two or more related statements. The execution starts from left to right.

That is all regarding Operators, Will update with more basic information in the future posts :)
 
Template designed using TrixTG