C Programming - Bitwise Operators - Discussion

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;
}

[A]. 4, 8, 0
[B]. 1, 2, 1
[C]. 12, 1, 12
[D]. 0, 0, 0

Answer: Option C

Explanation:

No answer description available for this question.

Neetu said: (Aug 21, 2010)  
4= 0100

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

4^8 = 1100 = 12

Imran said: (Dec 2, 2010)  
I am not understanding

Vennila.P said: (Jan 11, 2011)  
@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.

Roh said: (Jan 26, 2011)  
Answer is correct but no clear explanation.

So, can someone give detailed explanation?

Madhureddy said: (Feb 5, 2011)  
What vennila said is correct but you should perform & operation rather | operation.

Santhu said: (Feb 28, 2011)  
Vennila is correct.

Wikiok said: (Mar 13, 2011)  
& (bit AND) has in higher precedence than | (bit OR).So
4|8&8|4 == (4| (8&8) | 4) == ((4|8)|4) == (12|4) == 12

Atul Tailwal said: (Mar 25, 2011)  
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.

Raj said: (Apr 18, 2011)  
This explanation is good

Challenger said: (Aug 3, 2011)  
@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.

Simhadri said: (Apr 24, 2012)  
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.

Saiprabha said: (Sep 7, 2012)  
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

Neetu said: (Nov 23, 2013)  
#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?

Rahul said: (Apr 20, 2014)  
Bitwise & has more precedence comp then bitwise or why you first solve bitwise or please tell me.

Mithi said: (Jun 12, 2014)  
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.

Sudha said: (Nov 6, 2014)  
@Neetu.

The output is:

51 59 110

The correct or not.

Please anyone tell me.

Sana said: (Aug 8, 2015)  
51 59 110 is correct.

Sana said: (Aug 8, 2015)  
Can any one please tell me if i=4 and j=8 then how i&&j and i||j done?

Aalapini said: (Sep 5, 2015)  
Can some one explain i^j part? The function of ^ operator specifically.

Tarun Ghosh said: (Sep 24, 2015)  
^ this is binary XOR operator. Perform Bit-wise XOR operation Means odd no. of 1 is 1 otherwise 0. Then you will get your answer.

Lokesh said: (Dec 21, 2015)  
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.

Anish Kumar said: (Aug 28, 2016)  
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:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.