Subject-C Programming Year-FE Sem-II: Experiment No 4

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Subject-C Programming

Year-FE Sem-II

Experiment No 4

Aim: Write programs that demonstrates working of Logical and Bitwise operators.

Tools used: gedit, gcc, OS-Ubuntu 14.04.3 LTS

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.

Table: Truth table of logical AND, OR and NOT

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

All bit wise operations for x and y are given below.


1. x & y = 00000000 (binary) = 0 (decimal)
2. x | y = 01111000 (binary) = 120 (decimal)
3. ~ x=11111111111111111111111111 11111111111111111111111111111111010111=-41(decimal)
4. x ^ y = 01111000 (binary) = 120 (decimal)
5. x << 1 = 01010000 (binary) = 80 (decimal)
6. x >> 1 = 00010100 (binary) = 20 (decimal)

Algorithm: exp4a (Logical Operators)


Step 1: Start
Step 2: Declare and initialize variables a=40, b=20.
Step 3: is (a>b && a! =0)? Yes: goto step 4 else goto step 5
Step 4: print “&& Operator: Both conditions are true” and goto Step 10
Step 5: is (b>a || a=0) Yes: goto step 6 else goto step 7
Step 6: print "|| Operator: Only one conditions is true\n" and goto Step 10
Step 7: is (! (a>b && a! =0))? Yes: goto step 8 else goto step 9
Step 8: print "! Operator: Both conditions are true\n") and goto Step 10
Step 9: print "! operator Both conditions are true. But, status is inverted as false" and goto step 10.
Step 10: Stop

Algorithm: exp4b (Bitwise Operators)


Step 1: Start
Step 2: Declare Variable a, b, AND, OR, XOR, NOT;
Step 3: Print “Enter value of a and b
Step 4: Read variable a and b
Step 5: Compute, AND = (a&b);
OR = (a|b);
XOR = (a^b);
NOT = (~a);
Step 6: Display Result of Bitwise AND, OR, XOR, NOT, Left shift and Right shift operator.
Step7: Stop

Conclusion: Hence we have successfully studied working of Logical and Bitwise operators.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy