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.

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.

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

Similarly XOR follows the truth table

A

B

Output

0

0

0

0

1

1

1

0

1

1

1

0

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 aelse 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.

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

A

B

Output

0

0

0

0

1

0

1

0

0

1

1

1

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.

A

B

Output

0

0

0

0

1

1

1

0

1

1

1

1

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.

A

Output

0

1

1

0

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.

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.

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.

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.

Most times, we require interaction from the user, and we process the inputs given by them and display the outputs. Now have a look at the following program which does the function of adding two numbers. It takes two numbers from the user, adds it and displays the output.

// Getting inputs from user printf("Enter the first number : "); scanf("%d",&iNum1); printf("Enter the second number : ");\ scanf("%d",&iNum2);

// Process the inputs iSum = iNum1 + iNum2;

// Display the output printf("\nSum of %d and %d is %d",iNum1,iNum2,iSum);

getch();

}

Output

Enter the first number : 23Enter the second number : 12

Sum of 23 and 12 is 35

The program is straight forward, we first display the message to the user to input the first number, then the inbuilt function "scanf" will get the input from the user. The program execution pauses at this point until the user presses "Enter" key. Once the input is given, you will prompted to enter second number, and after getting the number, it will add the two numbers and displays the output.

Important thing to be noted here is the arguments passed in the Scanf function. First string parameter is used to define format specifier. It tells the compiler that it should treat the input from the user as specified in that format specifier. Format specifiers used in Scanf are similar to the one explained in this post. When I say %d in the scanf, it expects a integer. The next argument in the scanf is the variables where we need to store the data. We use scanf to store the value we enter in some memory location. So we need to pass the address of the variable as the argument. "&" operator in the scanf statement is the address of operator. &iNum1 will give the address of iNum1 variable. So the number that is got from user is stored in the address of iNum1.

Instead of using two different statements for obtaining the inputs, we can also use one single statement as shown below.

Code

printf("Enter two numbers : "); scanf("%d%d",&iNum1,&iNum2);

Output

Enter two numbers : 12 23

Sum of 12 and 23 is 35

As shown above, we need to separate the two inputs with a space/tab/new line, if we need to give the two inputs in the same scanf statement.

BLOG ARCHIVE

SUPPORT US

The contents posted here are from my understandings, and if you see any contradiction from what you have learnt, I will be glad to learn from you. Suggestions, Comments, Feedbacks are all welcomed. You can drop a mail to talktotrix[at]ymail[dot]com or can comment here in appropriate posts.