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.

Maheshkumar said:   2 decades ago
Do the operation from left to right.

Ani said:   1 decade ago
Can anyone explain this in a detailed way?

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.

Jalandhar said:   1 decade ago
Thans a lot @vijay

Javed said:   1 decade ago
Can anyone elaborate giving small example?

Sibaram said:   1 decade ago
Can't under stand please explain detail.

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)

Koushik said:   1 decade ago
Thanx vedhasree...!!!

Mani said:   1 decade ago
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:   1 decade ago
Thanks vedashree


Post your comments here:

Your comments will be displayed after verification.