Operators - C Programming Professional Made easy (2015)

C Programming Professional Made easys (2015)

Chapter 6 Operators

Operators are symbols that tell you to perform certain logical or mathematical manipulations. In the C language, there are plenty of built-in operators such as arithmetic, relational, logical, bitwise, assignment, and misc.

Arithmetic Operators

Let A = 10 and B = 20.

Operator Description Example

+ It adds two operands. A + B = 30

- It subtracts the second operand A – B = -10

from the first operand.

* It multiplies the operands. A * B = 200

/ It divides the numerator by the B / A = 2

denominator.

% It is a modulus operator. It gives the B % A = 0

remainder after an integer division.

++ It is an increments operator. It A++ = 11

increases the value of the integer

by one.

-- It is a decrements operator. It A-- = 9

decreases the value of the integer

by one.

Relational Operators

Let A = 10 and B =20.

Operator

Description

Example

==

It checks whether or not the values of both operands are equal. If they are equal, then the condition is true.

(A == B) is not true.

!=

It checks whether or not the values of both operands are equal. If they are not equal, then the condition is true.

(A != B) is true.

>

It checks whether or not the value of the left operand is bigger than the value of the right operand. If it is, then the condition is true.

(A > B) is not true.

<

It checks whether or not the value of the left operand is less than the value of the right operand. If it is, then the condition is true.

(A < B) is true.

>=

It checks whether or not the value of the left operand is bigger than the value of the right operand. If it is, then the condition is true.

(A >= B) is not true.

<=

It checks whether or not the value of the left operand is less than or equal to the value of the right operand. If it is, then the condition is true.

(A <= B) is true.

Logical Operators

Let A = 1 and B = 0.

Operator

Description

Example

&&

It is known as the Logical AND Operator. If the operands are non-zero, the condition is true. Otherwise, it is false.

(A && B) is false.

||

It is known as the Logical OR Operator. If one of the two operands is non-zero, the condition is true.

(A || B) is true.

!

It is known as the Logical NOT Operator. It is used to reverse the logical state of the operand. If the condition is true, the Logical NOT Operator will make it false.

!(A && B) is true.

Bitwise Operators

These operators work on bits and perform bit-by-bit operation. This is the Truth Table for |, ^, and &.

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

If A = 6 and B = 13:

A = 00111100

B = 00001101

----------------

A & B = 00001100

A | B = 00111101

A ^ B = 00110001

~A = 11000011

Let A = 60 and B = 13.

Operator

Description

Example

&

It is known as the Binary AND Operator. It copies a bit if it exists in both operands.

(A & B) = 12, which is equivalent to 00001100 in binary.

|

It is known as the Binary OR Operator. It copies a bit if it exists in either one of the operands.

(A | B) = 61, which is equivalent to 00111101 in binary.

^

It is known as the Binary XOR Operator. It copies a bit if it exists in just one operand.

(A ^ B) = 49, which is equivalent to 00110001 in binary.

~

It is known as the Binary Ones Complement Operator. It is unary and has the flipping bits effect.

(~A) = -60, which is equivalent to 11000011 in binary.

<<

It is known as the Binary Left Shift Operator. The value of the left operand is moved to the left depending on how many

A << 2 = 240, which is equivalent to 11110000 in binary. bits the right operand specified.

>>

It is known as the Binary Right Shift Operator. The value of the left operand is moved to the right depending on how many bits the right operand specified.

A >> 2 = 15, which is equivalent to 00001111 in binary.

Assignment Operators

Operator

Description

Example

=

It is an assignment operator. It allocates values from the right operand to the left operand.

C = A + B assigns the value of A + B into C.

+=

It is known as the add AND assignment operator. It adds the right operand to the left operand and then assigns the value to the left operand.

C += A is equal to C = C + A

-=

It is known as the subtract AND assignment operator. It subtracts the right operand from the left operand and then assigns the value to the left operand.

C -= A is equal to C = C – A

*=

It is known as the multiply AND assignment operator. It multiplies the right operand with the left operand and then assigns the value to the left operand.

C *= A is equal to C = C * A

/=

It is known as the divide AND assignment operator. It divides the left operand with the right operand and then assigns the result to the left operand.

C /= A is equal to C = C/A

%=

It is known as the modulus AND assignment operator. It takes modulus using two operands and assigns the result to the left operand.

C %= A is equal to C = C % A

<<=

It is known as the left shift AND assignment operator

C <<= 2 is equal to C = C << 2

>>=

It is known as the right shift AND assignment operator.

C >>= 2 is equal to C = C >> 2

&=

It is known as the bitwise AND assignment operator.

C &= 2 is equal to C & 2

^=

It is known as the bitwise exclusive OR and assignment operator.

C ^= 2 is equal to C ^ 2

|=

It is known as the bitwise inclusive OR and assignment operator.

C |= 2 is equal to C | 2