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

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

Laxman said:   7 years ago
In function data passing, expressions are evaluated from right to left,but printing results from left to right, so in the given problem first (i^j) evaluated,

i=0100
j=1000
i^j=1100 equivalent decimal is 12.

Then, next expression is (i|j&j|i)
the expression having bitwise AND and OR, in this bitwise AND having higher priority than bit wise OR, so grouping made as i|(j&j)|i, in this first (j&j) evaluated.

j=1000
j=1000
j&j=1000.

The remaining expression made as i|(1000)|i; here bitwise OR having two times that means same priority now we have to consider associativity. For bitwise operators left to right.

i=0100
1000
res=1100
1000

Final res=1100 decimal value 12.
third also same as the second expression so the result is 12.

Now printing the results in 12 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.

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

Vedhashree said:   1 decade ago
i= 4 =>0100 ; j=8 =>1000
solution for : i|j&j|i

& ->AND has highest Precedence than | ->OR operator hence should be evaluated first and then from left to right as usual .


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
(4)

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

Vasantha said:   10 years ago
4 value in binary is: 0100.

8 value in binary is: 1000.

4|8 = 0100|1000 = 1100 (According to the truth table of AND).

8|4 = 1000|0100 = 1100 (According to the truth table of OR).

12 value in binary is: 1100.

12 & 12 = 1100 & 1100 = 1100.

And 4^8 = 0100^1000 = 1100 = 12 (According to the truth table of XOR).
(2)

Selvaraj.v said:   1 decade ago
Do the bitwise operation from left to right

Binary form of 8 is 1000 and 4 is 0100

step:1 j=1000&1000=1000
step:2 i|j=0100|1000=1100
step:3 decimal value of 1100 is 12
step:4 do the same operation i|j&j|i value is=12
step:5 do the XOR operation i^j:0100^1000=1100=12
step:6 answer is 12 12 12

Tamojit Pal said:   1 decade ago
i=4 (binary-0100)
j=8 (binary-1000)
i|j&j|i...12(why)?
0100|1000=1100(by,bitwise OR operation)
1000|0100=1100(by,bitwise OR operation)
1100&1100=1100(12)(by,bitwise AND operation)

same as...i|j&j|i..12

and..
i^j...12(why)?
0100|1000=1100(by, bitwise XOR operation)
=12

Vijay said:   1 decade ago
Here &, | and ^ are bitwise operators,then operations will be performed on operands at bit level.

& - AND , | - OR , ^ - XOR operators

(4)d = (0000100)b, (8)d = (0001000)b

& has higher precedence over |

By considering all these, you will get the answer as expected.


Post your comments here:

Your comments will be displayed after verification.