Subject-C Programming Year-FE Sem-II: Experiment No 4
Subject-C Programming Year-FE Sem-II: Experiment No 4
Subject-C Programming Year-FE Sem-II: Experiment No 4
Year-FE Sem-II
Experiment No 4
Aim: Write programs that demonstrates working of Logical and Bitwise operators.
Theory :
I) Logical operators in C:
These operators are used to perform logical operations on the given two expressions or variables.
There are 3 logical operators in C language.
1. logical AND (&&)
2. logical OR (||)
3. logical NOT (!)
Following table shows the results of logical AND, OR, NOT operators with inputs A and B.
Try the following example to understand the concept of logical operators in C Programming.
a) && (Logical AND)
It returns true when both conditions are true.
Example:
If c = 5 and d = 2 then, expression ((c == 5) && (d > 5)) equals to 0.
b) || (logical OR)
It returns true when at-least one of the condition is true.
Example:
If c = 5 and d = 2 then, expression ((c == 5) || (d > 5)) equals to 1.
c) ! (logical NOT)
It reverses the state of the operand
Example:
If c = 5 then, expression ! (c == 5) equals to 0.
II) Bitwise operators in C:
● These operators are used to perform bit operations. Decimal values are converted into binary values
which are the sequence of bits and bit wise operators work on these bits.
● Bit wise operators in C language are
a) & (bitwise AND)
b) | (bitwise OR)
c) ~ (bitwise NOT)
d) ^ (XOR)
e) << (left shift)
f) >> (right shift)
Truth table for bit wise operation and Bit wise operators:
Example:
Consider x=40 and y=80. Binary forms of these values are given below.
x= 00101000
y= 01010000
Conclusion: Hence we have successfully studied working of Logical and Bitwise operators.