Download presentation
Presentation is loading. Please wait.
Published byHolly Tracey Flowers Modified over 8 years ago
1
C Operators
2
CONTENTS CONDITIONAL OPERATOR SIMPLE ASSIGNMENT OPERATOR COMPOUND ASSIGNMENT OPERATOR BITWISE OPERATOR OPERATOR PRECEDENCE
3
Conditional Operator (ternary) ?: The first operand/expression is evaluated, and its value determines whether the second or third operand/expression is evaluated: 1.If the value is true, the second operand/expression is evaluated. 2.If the value is false, the third operand/expression is evaluated. The result is the value of the second or third operand/expression. The syntax is: First operand ? second operand : third operand size != 0 ? size : 0 BACK TO INDEX
4
The compound assignment operators consist of a binary operator and the simple assignment operator. They perform the operation of the binary operator on both operands and store the result of that operation into the left operand. The following table lists the simple and compound assignment operators and expression examples:
5
Simple Assignment Operator = The simple assignment operator has the following form: lvalue = expr The operator stores the value of the right operand expr in the object designated by the left operand lvalue. The left operand must be a modifiable lvalue. The type of an assignment operation is the type of the left operand. i = 5 + x; BACK TO INDEX
6
Compound Assignment Operator ExampleEquivalent expression identifier operator= entity represents identifier = identifier operator entity += X+=7X= X+7 -= X-=7X= X-7 *= X*=7X= X*7 /= X/=7X= X/7 %=X%=7X= X%7 BACK TO INDEX
7
Bitwise (complement) NOT Operators ~ The ~ (bitwise negation) operator yields the bitwise (one) complement of the operand. In the binary representation of the result, every bit has the opposite value of the same bit in the binary representation of the operand. The operand must have an integral type. The result has the same type as the operand but is not an lvalue (left value). The symbol used called tilde. Suppose byNum variable represents the decimal value 8. The 16-bit binary representation of byNum is: 00000000 00001000 The expression ~byNum yields the following result (represented here as a 16-bit binary number): 11111111 11110111
8
Bitwise Shift Operators << Left shift operator, shift their first operand left ( << ) by the number of positions specified by the second operand. nbits << nshiftSize >> Right shift operator, shift their first operand right ( >> ) by the number of positions specified by the second operand. nbits >> nshiftSize Both operands must be integral values. These operators perform the usual arithmetic conversions; the type of the result is the type of the left operand after conversion. For leftward shifts, the vacated right bits are set to 0. For rightward shifts, the vacated left bits are filled based on the type of the first operand after conversion. If the type is unsigned, they are set to 0. Otherwise, they are filled with copies of the sign bit. For left-shift operators without overflow, the statement: expression1 << expression2 is equivalent to multiplication by 2 expression2. For right-shift operators: expression1 >> expression2 is equivalent to division by 2 expression2 if expression1 is unsigned or has a nonnegative value. The result of a shift operation is undefined if the second operand is negative, or if the right operand is greater than or equal to the width in bits of the promoted left operand. Since the conversions performed by the shift operators do not provide for overflow or underflow conditions, information may be lost if the result of a shift operation cannot be represented in the type of the first operand after conversion.
10
Bitwise AND Operator & The & (bitwise AND ) operator compares each bit of its first operand to the corresponding bit of the second operand. If both bits are 1's, the corresponding bit of the result is set to 1. Otherwise, it sets the corresponding result bit to 0. The bitwise AND ( & ) should not be confused with the logical AND. ( && ) operator. For example: 1 & 4 evaluates to 0 ( 0001 & 0100 = 0000 ) while 1 && 4 evaluates to true [ True && True = True ]
12
Bitwise XOR Operator ^ The bitwise exclusive OR operator ( the ^ symbol ) compares each bit of its first operand to the corresponding bit of the second operand. If both bits are 1 's or both bits are 0 's, the corresponding bit of the result is set to 0. Otherwise, it sets the corresponding result bit to 1.
14
Bitwise (Inclusive) OR Operator | The | (bitwise inclusive OR ) operator compares the values (in binary format) of each operand and yields a value whose bit pattern shows which bits in either of the operands has the value 1. If both of the bits are 0, the result of that bit is 0 ; otherwise, the result is 1. The bitwise OR ( | ) should not be confused with the logical OR ( || ) operator. For example: 1 | 4 evaluates to 5 ( 0001 | 0100 = 0101 ) while 1 || 4 ( True || True = True ) evaluates to true BACK TO INDEX
16
Precedence and Associativity of C Operators SymbolType of OperationAssociativity [ ] ( ). –> postfix ++ and postfix –– ExpressionLeft to right prefix ++ and prefix –– sizeof & * + – ~ ! UnaryRight to left typecasts UnaryRight to left * / % MultiplicativeLeft to right + – AdditiveLeft to right > Bitwise shiftLeft to right = RelationalLeft to right == != EqualityLeft to right & Bitwise-ANDLeft to right ^ Bitwise-exclusive-ORLeft to right | Bitwise-inclusive-ORLeft to right && Logical-ANDLeft to right || Logical-ORLeft to right ? : Conditional-expressionRight to left = *= /= %= += –= >= &= ^= |= Simple and compound assignment Right to left, Sequential evaluationLeft to right BACK TO INDEX
17
BACK TO INDEX
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.