C Programming - Structures, Unions, Enums - Discussion

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;
}

[A]. 12, 12, 12
[B]. 112, 1, 12
[C]. 32, 1, 12
[D]. -64, 1, 12

Answer: Option A

Explanation:

No answer description available for this question.

Maheshkumar said: (Jul 27, 2010)  
Do the operation from left to right.

Ani said: (Aug 31, 2010)  
Can anyone explain this in a detailed way?

Vijay said: (Aug 31, 2010)  
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.

Jalandhar said: (Nov 4, 2010)  
Thans a lot @vijay

Javed said: (Dec 2, 2010)  
Can anyone elaborate giving small example?

Sibaram said: (Dec 3, 2010)  
Can't under stand please explain detail.

Vedhashree said: (Dec 13, 2010)  
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

Koushik said: (Jan 8, 2011)  
Thanx vedhasree...!!!

Mani said: (Jan 17, 2011)  
i=4,j=8
|,&,^-these r bitwise operators so first we convert the iand j value to binary.
i=0100 j=1000
step1:
qus=i|j@j|i?
i|j=0100|1000=1100
j|i=1000|0100=1100
finally
i|j@j|i=1100&1100=1100=12

so the ans is 12 12 12

Syed said: (Jan 20, 2011)  
Thanks vedashree

Vikas said: (Feb 3, 2011)  
Thanks vedhashree.

Selvaraj.V said: (Feb 8, 2011)  
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

Prerna said: (Feb 24, 2011)  
Thanx Vedashree

Monica said: (Feb 24, 2011)  
Thanx Mani

Alok said: (Jun 27, 2011)  
Lot's of thank's all the people those who given the answer of this question.

Atul Jain said: (Jul 8, 2011)  
Thanks Vedhashree.

Tamojit Pal said: (Jul 23, 2011)  
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

Karthika said: (Jul 23, 2011)  
Then, which operator is used to denote "power-off" ?

Don't we use ^ ?

Abinandank said: (Aug 12, 2011)  
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

Ramdas said: (Aug 24, 2011)  
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

Levin said: (Sep 13, 2011)  
@Abinandank :Thanks for spending your valuable time with us. Its been very useful.

Arvind &Amp;Himanshu said: (Sep 20, 2011)  
Thanks vadhashree.

For brief explanation. :-).

Safi said: (Dec 30, 2011)  
if i|j =12
and i^j=12 then

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

Rupinderjit said: (Jan 3, 2012)  
@safi

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

Kumarreddy said: (Mar 2, 2012)  
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: (Mar 31, 2012)  
The output of XOR is 1 if 2 inputs are similar.

Sk.Mosin said: (Jun 23, 2012)  
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: (Jan 20, 2013)  
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: (Apr 30, 2013)  
@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.

Ananya said: (Jul 31, 2013)  
But doesn't an int store 16 bits?

Then why is everyone taking only 4 bits in hand?

Because if you consider 4 as 00000000 00000100 and 8 as 00000000 00001000 then while you XOR them it makes a difference!

Vanaja said: (Sep 5, 2013)  
Binary value of 4 is 0100.

Binary value of 8 is 1000.

Using OR operation compare it we get 1100 it is equal to 12.

Again we got 12 by comparison, compare both binary values of using AND operator we got the answer.

Abhishek said: (Mar 5, 2014)  
There is any other method available so computation will become fast.

As such that we need not to convert the number in binary first and direct get the answer.

Madhu said: (Dec 9, 2014)  
I can't understand what is in printf statement. Can you explain?

Priyanka said: (Jan 11, 2015)  
Simple AND and OR operation take place here.

In printf statement there is %d which asking for the decimal value of the result. And the decimal of 4 is 0100. And of 8 is 1000.

Now, by OR them we get 1100 which is the binary representation of 12 and hence the result follows.

Parth said: (May 17, 2015)  
The first two have to be same and there is only one matching option to that, so common sense if not logic.

Vasantha said: (Sep 11, 2015)  
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).

Jeva said: (Nov 27, 2015)  
Another thing to notice is that of the three printed numbers the first two *have* to be the same whatever they are. Only A satisfies this.

Kavi said: (Jan 5, 2016)  
Anyone can explain?

What is the difference between i&j and i&&j?

Sushant said: (May 30, 2016)  
@Kavi.

& is a bitwise operator.

Whereas;

&& is a logical operator.

Shakshi said: (Jun 20, 2016)  
Thanks, @Vadhashree. it's a clear explanation.

Mindmaster said: (Jul 31, 2016)  
Operators of same precedence are executed from left to right.

Umesh Pandey said: (Sep 25, 2016)  
Tricky method.

i|j&j|i, i|j&j|i gives same value.

The option is only 12,12 ie A.

Sonali said: (Oct 1, 2016)  
Good explanation @Abinandank.

Sujithra said: (Jan 16, 2017)  
What will be the answer when 1100&1000 occur in a program? please explain in detail.

Earl said: (Mar 8, 2017)  
Thanks to all your explanations.

Raji said: (Mar 16, 2017)  
Thank you to all your answers and explanation.

Raghavi said: (Mar 16, 2017)  
Thanks for the explanation friends. It helps me to understand easily.

Nilesh said: (Apr 9, 2017)  
@Sujithra.

The ans will be 1000.

In &(AND) operation,
1 & 1=1.
1 & 0=0.
0 & 1=0.
0 & 0=0.

Chandru said: (Sep 13, 2017)  
Thanks @Vedhashree.

Anchan said: (Sep 25, 2017)  
Thanks @Vedhashree.

Manoja V. said: (Oct 24, 2017)  
Thanks @Vedhashree.

Ramya said: (Dec 7, 2017)  
Thanks @Vedhashree.

Harshada said: (Mar 16, 2018)  
Not understood. Please help me.

Ravi said: (Oct 22, 2018)  
Good explanation, Thanks. @Ramadas.

Laxman said: (Jan 20, 2019)  
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.

Sanket said: (Apr 11, 2019)  
Thanks @Vedhashree.

Swaroop said: (Jun 14, 2019)  
Thanks @Vedashree.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.