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;
}
Discussion:
25 comments Page 1 of 3.
Neetu said:
1 decade ago
4= 0100
8= 1000 it will give 4|8 = 1100 & 1100 = 1100 equals to 12
4^8 = 1100 = 12
8= 1000 it will give 4|8 = 1100 & 1100 = 1100 equals to 12
4^8 = 1100 = 12
Imran said:
1 decade ago
I am not understanding
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.
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.
Roh said:
1 decade ago
Answer is correct but no clear explanation.
So, can someone give detailed explanation?
So, can someone give detailed explanation?
Madhureddy said:
1 decade ago
What vennila said is correct but you should perform & operation rather | operation.
Santhu said:
1 decade ago
Vennila is correct.
Wikiok said:
1 decade ago
& (bit AND) has in higher precedence than | (bit OR).So
4|8&8|4 == (4| (8&8) | 4) == ((4|8)|4) == (12|4) == 12
4|8&8|4 == (4| (8&8) | 4) == ((4|8)|4) == (12|4) == 12
Atul tailwal said:
1 decade ago
The solution is : OR(|) OPERATOR rules-> 1 1 =0, 1 0=1
AND(&) OPERATOR ->1 1 = 1, 1 0 = 0, 0 1= 0
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.
AND(&) OPERATOR ->1 1 = 1, 1 0 = 0, 0 1= 0
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.
Raj said:
1 decade ago
This explanation is good
Challenger said:
1 decade ago
@All.
in i|j&j|i
j&j wiill be evaluated first because of the order of precedence (& more than |)
The answer will remain unchanged but that might not be the case in another situation.
The right way is
0100|1000&1000|0100 (&)
-> 0100|1000|0100 (leftmost|)
1100|0100
1100
12.
in i|j&j|i
j&j wiill be evaluated first because of the order of precedence (& more than |)
The answer will remain unchanged but that might not be the case in another situation.
The right way is
0100|1000&1000|0100 (&)
-> 0100|1000|0100 (leftmost|)
1100|0100
1100
12.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers