C C++

C Operators


C categorizes the operators into the following groups:

  1. Arithmetic Operators
  2. Assignment Operators
  3. Comparison Operators
  4. Logical Operators
  5. Bitwise Operators

Arithmetic Operators

Operator Name Description Example
+ Addition Adds together two values x + y
- Subtraction Subtracts one value from another x - y
* Multiplication Multiplies two values x * y
/ Division Divides one value by another x / y
% Modulus Returns the division remainder x % y
++ Increment Increases the value of a variable by 1 ++x
-- Decrement Decreases the value of a variable by 1 --x

Assignment Operators

Operator Name Description Example Same As
= Assignment Assigns values from right side operands to left side operand x = 5 x = 5
+= Add AND Assignment It adds the right operand to the left operand and assign the result to the left operand. x += 3 x = x + 3
-= Subtract AND Assignment  It subtracts the right operand from the left operand and assigns the result to the left operand. x -= 3 x = x - 3
*= Multiply AND assignment It multiplies the right operand with the left operand and assigns the result to the left operand. x *= 3 x = x * 3
/= Divide AND assignment It divides the left operand with the right operand and assigns the result to the left operand. x /= 3 x = x / 3
%= Modulus AND assignment It takes modulus using two operands and assigns the result to the left operand. x %= 3 x = x % 3
&= Bitwise AND assignment Bitwise AND assignment x &= 3 x = x & 3
|= Bitwise inclusive OR and assignment Bitwise inclusive OR and assignment x |= 3 x = x | 3
^= Bitwise exclusive OR and assignment Bitwise exclusive OR and assignment x ^= 3 x = x ^ 3
>>= Right shift AND assignment Right shift AND assignment x >>= 3 x = x >> 3
<<= Left shift AND assignment Left shift AND assignment x <<= 3 x = x << 3

Comparison Operators

Operator Name Description Example
== Equal to Returns 1 if the values are equal x == y
!= Not equal Returns 1 if the values are not equal x != y
> Greater than Returns 1 if the first value is greater than the second value x > y
< Less than Returns 1 if the first value is less than the second value x < y
>= Greater than or equal to Returns 1 if the first value is greater than, or equal to, the second value x >= y
<= Less than or equal to Returns 1 if the first value is less than, or equal to, the second value x <= y

Logical Operators

Operator Name Description Example
&& AND Returns 1 if both statements are true x < 5 &&  x < 10
|| OR Returns 1 if one of the statements is true x < 5 || x < 4
! NOT Reverse the result, returns 0 if the result is 1 !(x < 5 && x < 10)

Bitwise Operators

Operator Name Description Example
& AND Binary AND Operator copies a bit to the result if it exists in both operands. (A & B) = 12, i.e., 0000 1100
| OR Binary OR Operator copies a bit if it exists in either operand. (A | B) = 61, i.e., 0011 1101
^ XOR Binary XOR Operator copies the bit if it is set in one operand but not both. (A ^ B) = 49, i.e., 0011 0001
~ First Complement Binary One's Complement Operator is unary and has the effect of 'flipping' bits. (~A ) = ~(60), i.e,. -0111101
<< LeftShift Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. A << 2 = 240 i.e., 1111 0000
>> Rightshilf Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. A >> 2 = 15 i.e., 0000 1111