Conditional Operators, Increment/Decrement and Bitwise perators
| Conditional Operators | ||
|---|---|---|
| Operator | Usage | Description |
| ? : | (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 | ||
|---|---|---|
| Operator | Use | Description |
| ++ (Increment) | i++ or ++i | Increment 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 --i | Decrement 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 | ||
|---|---|---|
| Operator | Usage | Description |
| = (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 | ||
|---|---|---|
| Operator | Meaning | Description |
| & (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 | B | Bit 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 ^ B | Bitwise 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) | ~A | Produces 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 >> B | Shift 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.



0 comments:
Post a Comment