ProgIntro

Operators

Arithmetic Operators

Operator Description Example
+ Adds two operands. A + B = 23
Subtracts second operand from the first. A − B = 17
* Multiplies both operands. A * B = 60
/ Divides numerator by de-numerator. A / B = 6
% Modulus Operator and remainder of after an integer division. A % B = 2

link to c program on arithmetic operators

Arithmetic Assignment Operators

link to c program on arithmetic assignment operators

Relational Operators

Operator Description Example
== Checks if the values of two operands are equal or not. If yes, then the condition becomes true. (A == B) is not true.
!= Checks if the values of two operands are equal or not. If the values are not equal, then the condition becomes true. (A != B) is true.
> Checks if the value of left operand is greater than the value of right operand. If yes, then the condition becomes true. (A > B) is not true.
< Checks if the value of left operand is less than the value of right operand. If yes, then the condition becomes true. (A < B) is true.
>= Checks if the value of left operand is greater than or equal to the value of right operand. If yes, then the condition becomes true. (A >= B) is not true.
<= Checks if the value of left operand is less than or equal to the value of right operand. If yes, then the condition becomes true. (A <= B) is true.

link to c program on relational operators

Logical Operators

Operator Description Example
&& Called Logical AND operator. If both the operands are non-zero, then the condition becomes true. (A && B) is false.
|| Called Logical OR Operator. If any of the two operands is non-zero, then the condition becomes true. (A || B) is true.
! Called Logical NOT Operator. It is used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false. !(A && B) is true.

Bitwise Operators

Bitwise operator works on bits and perform bit-by-bit operation. The truth tables for &, |, and ^ is as follows:

p q p & q p | q p ^ q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
Operator Description Example
& Binary AND Operator copies a bit to the result if it exists in both operands. (A & B) = 12, i.e., 0000 1100
| 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 One’s Complement Operator is unary and has the effect of ‘flipping’ bits. (~A ) = ~(60), i.e,. -0111101
« 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
» 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