C Programming - Bitwise Operators - Discussion

Discussion Forum : Bitwise Operators - Find Output of Program (Q.No. 12)
12.
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;
}
4, 8, 0
1, 2, 1
12, 1, 12
0, 0, 0
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
25 comments Page 3 of 3.

Madhureddy said:   1 decade ago
What vennila said is correct but you should perform & operation rather | operation.

Roh said:   1 decade ago
Answer is correct but no clear explanation.

So, can someone give detailed explanation?

Vennila.p said:   1 decade ago
@Imran

The solution is :

Binary format of 4 = 0100 and 8 = 1000

Therefore 4|8 is = 0100|1000 = 1100 = 12

Similary 8|4 is also 1100

now i|j & j|i=1100 & 1100 =1100=12

Then i|j && j|i =12&&12 condition is true .. so it return 1.

Then i^j = 0100^1000 = 1100 = 12.

Imran said:   1 decade ago
I am not understanding

Neetu said:   1 decade ago
4= 0100

8= 1000 it will give 4|8 = 1100 & 1100 = 1100 equals to 12

4^8 = 1100 = 12


Post your comments here:

Your comments will be displayed after verification.