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;
}
Discussion:
25 comments Page 3 of 3.
Lokesh said:
10 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.
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:
9 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;.
x=50 ++x=51;.
Hayyan said:
8 years ago
| what is this symbol means?
Yasaswini said:
8 years ago
@Sana.
X and y values are defined as constant.
So values don't change.
The answer is 50 60 110.
If it is wrong pls tell me how it comes?
X and y values are defined as constant.
So values don't change.
The answer is 50 60 110.
If it is wrong pls tell me how it comes?
VISHAL VANAKI said:
7 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.
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)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers