C Operations
C Operations
Pratik Chattopadhyay
Arithmetic Operators
Assume variable A holds 10 and variable B holds 20
! Called Logical NOT Operator. It is used to reverse the !(A && B) is true.
logical state of its operand. If a condition is true, then
Logical NOT operator will make it false.
| Binary OR Operator copies a bit if it exists in either operand. (A | B) = 61, i.e., 0011 1101
^ Binary XOR Operator copies the bit if it is set in one operand but not both. (A ^ B) = 49, i.e., 0011 0001
Binary Ones Complement Operator is unary and has the effect of 'flipping' (~A ) = -61, i.e., 1100 0011 in 2's
~
bits. complement form.
Binary Left Shift Operator. The left operands value is moved left by the
<< A << 2 = 240 i.e., 1111 0000
number of bits specified by the right operand.
Binary Right Shift Operator. The left operands value is moved right by the
>> A >> 2 = 15 i.e., 0000 1111
number of bits specified by the right operand.
Assignment Operators
Operator Description Example
= Simple assignment operator. Assigns values from right side operands to left side C = A + B will assign the value of A + B
operand to C
+= Add AND assignment operator. It adds the right operand to the left operand and
C += A is equivalent to C = C + A
assign the result to the left operand.
-= Subtract AND assignment operator. It subtracts the right operand from the left
C -= A is equivalent to C = C - A
operand and assigns the result to the left operand.
*= Multiply AND assignment operator. It multiplies the right operand with the left
C *= A is equivalent to C = C * A
operand and assigns the result to the left operand.
/= Divide AND assignment operator. It divides the left operand with the right
C /= A is equivalent to C = C / A
operand and assigns the result to the left operand.
%= Modulus AND assignment operator. It takes modulus using two operands and
C %= A is equivalent to C = C % A
assigns the result to the left operand.
<<= Left shift AND assignment operator. C <<= 2 is same as C = C << 2
>>= Right shift AND assignment operator. C >>= 2 is same as C = C >> 2
&= Bitwise AND assignment operator. C &= 2 is same as C = C & 2
^= Bitwise exclusive OR and assignment operator. C ^= 2 is same as C = C ^ 2
|= Bitwise inclusive OR and assignment operator. C |= 2 is same as C = C | 2
sizeof() and Conditional Ternary Operators
Operator Description Example
sizeof() Returns the size of a variable. sizeof(a), where a is integer, will return 4.
& Returns the address of a variable. &a; returns the actual address of the variable.
* Pointer to a variable. *a;
?: If Condition is true ? then value X :
Conditional Expression. otherwise value Y