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 2 of 3.

Mithi said:   1 decade ago
Does anybody know for sure whether:

1. & will be operated before |
(as I too have read that precedence is (high to low) !, &, |)

OR

2. | will be operated before &
(because of left to right associativity).

Kindly share your views.

Rahul said:   1 decade ago
Bitwise & has more precedence comp then bitwise or why you first solve bitwise or please tell me.

Neetu said:   1 decade ago
#include<stdio.h>
#define x 50
#define y 60
int main()
{

int z = ++x + --y;
printf("%d%d%d/n",x,y,z);
}

Can anyone explain me this program and its output?

Saiprabha said:   1 decade ago
Here i=4 and j=8
now first i|j & j|i
means 4|8 & 8|4
first 8 & 8
0 0 0 0 1 0 0 0
0 0 0 0 1 0 0 0
--------------------
0 0 0 0 1 0 0 0

that means output is 8 again

now 4|8
0 0 0 0 0 1 0 0
0 0 0 0 1 0 0 0
---------------------
0 0 0 0 1 1 0 0 output is 12

now 12|4
0 0 0 0 1 1 0 0
0 0 0 0 0 1 0 0
----------------------
0 0 0 0 1 0 0 0 output is 12

the entire output for m is 12
there is only option which has 12 so the answer is C

Simhadri said:   1 decade ago
4 = 0100
8 = 1000
Let taken 1=true and 0=false
| means "or" operator i.e. only false to false condition become false but in other cases become true condition.
& means "and" operator i.e. only true and true condition become true but in other cases it becomes false.
^ means "double implication" i.e. true, false condition and false, true conditions are becomes true but in other cases it shows false.
Then,
i|j = 0100 | 1000 = 1100 = 12
j|i = 1000 | 0100 = 1100 = 12
and then i|j&j|i = 1100 & 1100 = 1100 = 12
So first condition prints 12
And third condition also prints 12 (try it!)

In the second condition following '&&' is operator shows true prints 1 or false prints 0 for the following condition. So 12&12 = 12 so it is true so it prints 1.
Thats it.

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.

Raj said:   1 decade ago
This explanation is good

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.

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

Santhu said:   1 decade ago
Vennila is correct.


Post your comments here:

Your comments will be displayed after verification.