C Programming - Structures, Unions, Enums - Discussion

Discussion Forum : Structures, Unions, Enums - Find Output of Program (Q.No. 7)
7.
What will be the output of the program ?
#include<stdio.h>

int main()
{
    int i=4, j=8;
    printf("%d, %d, %d\n", i|j&j|i, i|j&j|i, i^j);
    return 0;
}
12, 12, 12
112, 1, 12
32, 1, 12
-64, 1, 12
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
58 comments Page 6 of 6.

Arvind &himanshu said:   1 decade ago
Thanks vadhashree.

For brief explanation. :-).

Safi said:   1 decade ago
if i|j =12
and i^j=12 then

How its operation will be perform same, while both are distinct bitwise operators ?

Rupinderjit said:   1 decade ago
@safi

Operation and bit manipulation is different, but coincidentally result is same, not a big issue.

Kumarreddy said:   1 decade ago
i=4,j=8;
step1:

i=4==>0010
j=8==>1000
=============
now we have to find i|j&j|i, i^j);
i|j&j|i==>0010 | 1000 & 1000 | 0100
solve & operator first
then we wil get
==>0100 | 1000 |0100
now solve from L -> R
==>1100 | 0100
==>1100
which is equal to 1*2^3+1*2^2+0*2^1+0*2^0
==>8+4+0+0
==>12
similarly remaing two also ................

Pavan said:   1 decade ago
The output of XOR is 1 if 2 inputs are similar.

Sk.Mosin said:   1 decade ago
Here i=4------>0100 binary form.

And j=8------>1000 binary form.

Let us take i!j means "i OR j" operation.

i.e., i j (i OR j).

0 1 1.
1 0 1.
0 0 0.
0 0 0.

That means 1100 is 12. and so on. Until the final step we get 12, 12, 12.

Kirti said:   1 decade ago
Basically ,meaning of AND operator is when 0&0 it returns 0 like this
0 0 =0
0 1=0
1 0=0
1 1=1

And for OR operator is
0 0=0
0 1=1
1 0=1
1 1=1

And for XOR operator is
0 0=1
0 1=0
1 0=0
1 1=0

Now our problem is i|j&j|i, i|j&j|i, i^j
So AND it has more precedence than all operator so it ll perform first,
i=4, j=8.

So its binary number is i=0100 n j=1000
So Step 1: 0100 | (1000 & 1000) | 0100

Becomes 0100 | 1000 | 0100
Step 2: (0100 | 1000) | 0100
Becomes 1100 | 0100
Step 3: (1100 | 0100)
Becomes 1100 which is equivalent to 12

Solution for i ^ j

Step 1: 0100 ^ 1000
Becomes 1100 which is equivalent to 12

Hence the answer 12,12,12.

Prasad said:   1 decade ago
@Kirti.

Your truth table for xor gate is wrong.

Truth table is:

A B output.

0 0 0.
0 1 1.
1 0 1.
1 1 0.


Post your comments here:

Your comments will be displayed after verification.