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;
}
Discussion:
58 comments Page 6 of 6.
Koushik said:
1 decade ago
Thanx vedhasree...!!!
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
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)
Sibaram said:
1 decade ago
Can't under stand please explain detail.
Javed said:
1 decade ago
Can anyone elaborate giving small example?
Jalandhar said:
1 decade ago
Thans a lot @vijay
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.
& - 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.
Ani said:
1 decade ago
Can anyone explain this in a detailed way?
Maheshkumar said:
2 decades ago
Do the operation from left to right.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers