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.

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.

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

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?

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

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.

Sudha said:   1 decade ago
@Neetu.

The output is:

51 59 110

The correct or not.

Please anyone tell me.

Sana said:   1 decade ago
51 59 110 is correct.

Sana said:   1 decade ago
Can any one please tell me if i=4 and j=8 then how i&&j and i||j done?

Aalapini said:   10 years ago
Can some one explain i^j part? The function of ^ operator specifically.
(1)

Tarun Ghosh said:   10 years ago
^ 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.


Post your comments here:

Your comments will be displayed after verification.