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 4 of 6.

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.

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.

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

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 ................

Rupinderjit said:   1 decade ago
@safi

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

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 ?

Arvind &himanshu said:   1 decade ago
Thanks vadhashree.

For brief explanation. :-).

Levin said:   1 decade ago
@Abinandank :Thanks for spending your valuable time with us. Its been very useful.

Ramdas said:   1 decade ago
Given data is int i=4, j=8;
i=4=0100
j=8=1000
|-> is bit wise OR
& -> is bit wise AND
^ -> is bit wise XOR
in sop(Structure Oriented Programing)Languages like c and oops(Object Oriented Programming)Languages like c++ and java the evaluation order is left to right

value of i|j&j|i

so first we have to calculate i|j
i=0100
j=1000
--------
1100=i|j
1000=j
---------
1000=i|j&j
0100=i
--------
1100=i|j&j|i=12
***************
next we have to calculate i^j
i=0100
j=1000
------
1100=i^j=12
so ans is 12,12,12

Abinandank said:   1 decade ago
Binary value of 8 is 1000 and 4 is 0100

consider i|j(An OR operation)
                                         Truth table
1000 val1 val2 ans(OR)ans(AND)ans(XOR)
0100 0 0 0 0 0
---- 0 1 1 0 1
1100 1 0 1 0 1
---- 1 1 1 1 0


so j|y yields the same 1100


if u perform i|j & j|i
1100
1100 &
----
1100
----

1100 is equivalent to decimal 12


for printf("%d, %d, %d\n", i|j&j|i, i|j&j|i, i^j);

ans: 12 12 i=1000
j=0100 ^( see ex-or truth table) ------
1100 equiv to decimal 12


Hence 12 12 12


Post your comments here:

Your comments will be displayed after verification.