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 :)

Operators in C - Part 2 0 comments

Continuing from Part 1

iv) Logical Operators : This operator is typically used for operating on bits. Or we can say that, it operates on other relational expressions.

Assume I have a scenario where I need to find the greatest of three numbers. What I will do first is, I will compare first two numbers and find out which is bigger. Then I take this bigger number and compare it with the third element. So in order to find a a greatest number, I need to make two comparisions, and in "both" the comparisions the number should be greater. Here comes the usefullness of && operator (Logical And)

Here is how we use it : (iNum1 > iNum2) && (iNum2 > iNum3) : If both the condition evaluates to true, then this expression evaluates to true. If any one fails, then it evaluates to false. Logical And follows the following truth table

ABOutput
000
010
100
111

Here the A and B specifies the two relational expressions. and the Output is the result of Logical end between the two relational expressions.

Now consider another scenario where I need to find whether I have scored above 90 in atleast one subject. What I will do, I will first compare my first subject mark with 90, if false then I will continue the same with other subjects until I get a subject that has over 90 marks. Now I will use the || operator (Logical Or).

Here is an example : (Mark1 >> 90) || (Mark2 >> 90) || (Mark3 >> 90). If "atleast" one condition is true, then the expression evaluates to true. The Logical Or follows the following truth table.

ABOutput
000
011
101
111

Now the last Logical operator is the Not Operator (!). It is used for negating an value. It is a unary operator, meaning it operates on only one operand unlike other two. Logical Not follows the following truth table.

AOutput
01
10

v) Increments and Decrement Operators : I have a scenario where I need to perform some set of operations N number of times. So what I will do, I will first assign a variable(say count) to 0. Then for each time the operation is performed, I will add one to it using the expression count=count+1 and then I will compare the value with N. Now people saw that addition/subtaction of one to a variable is more commonly used, and hence they have introduced a very handy operation called Increment and decrement operators.
These are unary operators as they are operating on single operand.
Code
   int iNum=0;

// Increment Operator
iNum++;

// Outputs 1
printf("%d", iNum);

// Decrement Operator
iNum--;

// Outputs 0
printf("%d", iNum);

Ok great, thats a nice little operator. But I need do the comparision, and I need to increment the variable. That is two statements. Why can't I do it in one single statement? C allows us to do something like (iNum++ > 5). Now consider the following example...
Code
   int iNum = 5;   
printf("%d", (iNum++ > 5));

Guessed the output ? No "1" (non zero) is a wrong answer. It will actually output 0 (false). What is the reason ? Because what we say as iNum++ is a post increment operator. The variable will be first utilised in the comparision and then the value of the variable will be incremented. Ok If there is something post, then there should be something called preincrement(++iNum). So if I print (++iNum > 5) in the above scenario it will display 1(non-zero). Similarly there is post decrement(iNum--) and pre decrement(--iNum).

If these increment and decrement operators comes individually in a statement, then post and pre-decrement doesn't make any difference. If it comes alone with some other expressions or in some functions as in the printf statement, it does makes a difference. I will make another detailed post regarding this increment and decrement operator to explain it better.

Lets have a break and continue this in Part 3

Operators in C - Part 1 0 comments

Now in order to make some advanced programs, we need to know about this basic thing called Operators. I have expained earlier about micro processor programming where we use statements like 2E 23,34 for instructing the processor to perform an add operation. But it is very low level, and it is difficult for us. So better approach which C give us is Operators.

In general terms, an operator is a function that specify specific actions on objects. Those objects which acts as an input to the operator is called an operand. Let us see the different types of operators used in C. The following are categorised based on the operation they perform.

i) Arithmetic Operators : These operators are used for performing arithmetic operations like Addition, Subtraction, Multiplication, Division, Modulus Operator.
Code
 iNum3 = iNum1 + iNum2; // Addition operation 
iNum3 = iNum1 - iNum2; // Subtraction operation
iNum3 = iNum1 * iNum2; // Multiplication operation
iNum3 = iNum1 / iNum2; // Division operation
iNum3 = iNum1 % iNum2; // Modulo operation (Gives remainder of division operation - iNum1/iNum2)

While the first four operators can be used for floating point numbers also, modulo operators can be used only for Integers.

ii) Assignment Operators : If you look at the above code, you could see that we have used "=". This is called the assignment operator. What it does is, evaluates the expression on the right hand side and assigns the result of the evaluation to the operand in the left hand side. Hence the left hand operand cannot be an expression.

There are also some special assignment operator called Short hand operator.
Code
 iNum1 += iNum2; // Similar to iNum1 = iNum1 + iNum2
iNum1 -= iNum2; // Similar to iNum1 = iNum1 - iNum2
iNum1 *= iNum2; // Similar to iNum1 = iNum1 * iNum2
iNum1 /= iNum2; // Similar to iNum1 = iNum1 / iNum2
iNum1 %= iNum2; // Similar to iNum1 = iNum1 % iNum2

This is pretty much useful while we need to store the result in one of the operands.

iii) Relational operators : These operators are used for comparing two operands. The result of the operation is either true(1 or non zero value) and false(0). It is used in conditional statements where we need to perform certain set of operation if a condition is true, or another set of operations if a condition is false.
Code
      int iNum1=10;
int iNum2=20;

// Checks whether iNum1 is equal to iNum2
printf("%d\n",iNum1 == iNum2);

// Checks whether iNum1 is greater than iNum2
printf("%d\n",iNum1 > iNum2);

// Checks whether iNum1 is greater than or equal to iNum2
printf("%d\n",iNum1 >= iNum2);

// Checks whether iNum1 is lesser than iNum2
printf("%d\n",iNum1 < iNum2);

// Checks whether iNum1 is lesser than or equal to iNum2
printf("%d\n",iNum1 <= iNum2);

// Checks whether iNum1 is not equal to iNum2
printf("%d\n",iNum1 != iNum2);

Output
0
0
0
1
1
1

This post is getting pretty big, so lets continue in my next post on operators :)
 
Template designed using TrixTG