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

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.

VISHAL VANAKI said:   6 years 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 a 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 become 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.

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.
That's it.
(2)

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

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.

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.

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.

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.

Lokesh said:   9 years ago
For i|j&&j|i or operator has high precedence so j|i=12 and i|j=12.

Result of i|j && result of j|i is true because in && operator it checks both operands are non zero.

So it produces 1 as its output.

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?

Anish Kumar said:   8 years ago
An unary operator has higher precedence than arithmetic operator so why pre-increment and pre-decrement will happen first ++x + --y i.e. 51 + 59 = 110.

x=50 ++x=51;.


Post your comments here:

Your comments will be displayed after verification.