Conditional Operators, Increment/Decrement and Bitwise perators

Conditional Operators
OperatorUsageDescription
? :(Condition)?(Expr 1):(Expr 2);
Condition will be tested and if it is true then expression 1 will be evaluated, if the the condition is false then expression 2 will be executed.

Notes
  • Conditional operator is a ternary operator as because it takes 3 operands.
  • Conditional operator is used for making decisions. A test condition is evaluted and if it is true then the expression or value immediately follows the ? will get executed, and if the condition is false, then expression or value after the : will get executed.

Increment/Decrement Operators
OperatorUseDescription
++ (Increment)i++ or ++iIncrement the value of i by 1. If the value of i is 1, then
after execution of i++, value of i will be 2.
-- (Decrement)i-- or --iDecrement value of i by 1.

Notes
  • Increment and Decrement operators are unary operators and which takes only one operand
  • Increment/Decrement operator can be used in place where we need to increment the value of an operand by one. For example i=i+1 can be replaced with i++, similary i-- can be used instead of i=i-1
  • These operators can take two forms : Post increment/Post decrement and Pre increment/Pre decrement. If an increment/decrement operator is used before the variable then it is pre increment/pre decrement operator and if it is written after the variable then it is called post increment/post decrement.
  • The difference between the pre and post operator occurs when it is used in an expression. In pre incrementing/pre decrementing the value of the variable is incremented/decremented first and then new value is taken with the expression. In post incrementing/decrementing, the value of the variable is incremented/decremented only after evaluation of execution. So the current value of variable will be used with the expression and after its execution the value will be incremented or decremented

Assignment/Short hand Operators
OperatorUsageDescription
= (Assignment)a=5; b=i+10;Used to assign the value to a variable. Value 5 will be assigned to a and likewise resultant value of the expression i+10 will be assigned to b.
+=a+=2;Short hand assignment operation. Value 2 will be added with a and the result stored in a and is similar to the expression a=a+2.
-=a-=b;
Subtract b from a and store the result in a, similar to a=a-b.
*=a*=a;
Square the value of a and store it in a (a=a*a).
/=a/=2;
Divide a by 2 and store the quotient in a (a=a*2).
%=a%=b;
Find the remainder of the operation a/b and store the remainder in a (a=a%b).

Notes
  • +=,-=, *=, /=, %= is also known as short hand operators.

Bitwise Operators
OperatorMeaningDescription
& (Bitwise AND)A & B
It will check the binary values of A and B, and result will be 1 corresponding bit positions of both the binary values are 1 otherwise it will be 0. For example : 01011 & 11010 => 01010
| (Bitwise inclusive OR)A | BBit value is set to 1 if either binary bit values are 1 or if any one bit is 1, otherwise it will be set to 0. For example : 01011 | 11010 => 11011
^ (Bitwise exclusive OR)A ^ BBitwise exclusive OR operator sets a 1 in each bit positions where its operands have different bits, if the bit values are same then it will set a 0. For example : 01011 ^ 11010 => 10001
~ (1's complement)~AProduces 1's complement of an integer, this operator Will invert bit values. 1' complement operator will change the bits 0 to 1 and 1 to 0. For example : ~01011 => 10100
<< (Left shift)A << B Shift bits of A by B distance to the left, shifted bits will be lost and vaccated least significant bits will be filled with 0's .
>> (Right shift)A >> BShift bits of A by B distance to the right, shifted bits will be lost and vaccated most significant bits will be filled with 0's .

Notes
  • Bitwise operators make C unique in high level languages, and this is because C is known as high level language with low level features.

Previous
Next

Operators in C

Arithmetic Operators
OperatorDescriptionExample
+To add two numeric valuesa+b
-Used for subtractiona-b
*To multiply two numeric valuesa*b
/Used for divisiona/b
% (Modulo operator)To compute the remainder of a division operationa%b

Notes
  • An arithmetic operation on two integer values always yields an integer result.
  • Operation between float and float yields a float result.
  • Operation on integer and a float value will results a float result
  • % operator cannot be used with float or double values

Relational Operators
OperatorUseDescription
>a>bExpression return true if a is greater than b
>=a>=bTrue if a is greater than or equal to b
<a
True if a is less than b
<=a<=bTrue if a is less than or equal to b
==a==bTrue if a is equal to b
!=a==bTrue if a and b are not equal

Notes
  • Relational operators are comparison operators which will compare two values and will return true or false.
  • Relational operators are used in conditional expressions.
  • == is an relational operator to check the equality of two values, but a single = operator is assignment operator.

Logical Operators
OperatorMeaningDescription
&&Logical ANDCombine to conditional expressions, and will return true if both conditional expressions are true otherwise it will return false.
||Logical ORCombine to conditional expressions, and will return false if both conditional expressions are false otherwise it will return true.
!Logical NOT
Negate a conditional expression

Notes
  • Logical AND operator is used to combine two conditions, and the whole expression that is connected by the AND (&&) operator will return only true if both conditions are true, otherwise it will return false.
  • Logical OR operator is used to combine two conditions, and the whole expression that is connected by the OR (||) operator will return true if any one condition is true, otherwise it will return false.
  • Logical NOT operator is used to negate the conditional value, if a condition 'CON' yields a true then !CON will return false, if condition CON is false then !CON will return true.

Previous
Next

Operators and Expressions

All processes are done at the Arithmetic & Logic Unit of the computer - ALU. All operations or processes are performed as arithmetic & Logic operations in ALU. C is rich with its efficient operators, using which we can easily write efficient programs.

Operator is nothing but a symbol which instructs the computer what operation it has to perform.

In machine languages, an operator may be a combination of binary 0's and 1's, i.e. there will be a unique 0's and 1's combination for each operations, say for addition, subtraction, comparison etc. In Assembly language we uses the instruction set to perform the operations, for addition operation we may use ADD instruction, similarly CMP instruction can be used for comparison. In modern high level languages, we can use symbols similar to those used in maths or real life to instruct the computer for a particular operation. For example '+' symbol can be used to instruct the computer for an addition operation, similarly '-','*','/' symbols can be used to perform the other 3 arithmetic operations.

An operator tells the computer what to do with the associated operands. Operands are the memory values that we need to manipulate to get the results. So our instructions would be a combination of operators and operands, and such a combination is known as an expression. For example sum=a+b is an expression and contain 3 operands - sum, a, b and two operators '+' and '='. Here + operator directly deal with two operands (a and b) and is used to add two values, so '+' (Addition) operator is a binary operator. An operator is a binary operator if it deal with or uses two operands for operations. '-' (subtraction) operator is a binary operator and uses two operands - minuend and subtrahend, '-' operator can also be used to represent a negative value, say -10, here '-' is associated with only one operand is a 'unary minus'/ unary operator.

Operators are one of the striking feature of C. C supports different types of operators.

Operators in C

1. Arithmetic Operators [ + , - , *, /, % ]

2. Relational Operators [ > , < , >=, <=, ==,!= ]

3. Logic Operators [ &&, ||, ! ]

4. Assignment Operators [ =, +=, -=, *=, /= ]

5. Conditional Operators [ ?, : ]

6. Increment/Decrement Operators [ ++, --]

7. Bitwise Operators [ &, ^, ~, >>, << ]

8. Special Operators [ ,  .  sizeof() -> ]

Previous
Next

Design by The Blogger Templates The Blog Full of Games

© CourseZone
2008 - 2010 All Rights Reserved.