0

Explain the output

#include<stdio.h> void main() { int i=32,j=0x20,k,l,m; k = i|j ; l = i&j ; m = k^l ; printf("%d %d %d %d %d",i,j,k,l,m); }

24th Dec 2017, 5:47 AM
Sarthak
Sarthak - avatar
2 Answers
+ 3
2/2 int k is determined by i OR j. This is represented by 00100000 | 00100000. Since i and j are the same, and anything OR itself is the same, the result for k is 00100000. int l is determined by i AND j. This is represented by 00100000 & 00100000. Since i and j are the same, and anything AND itself is the same, the result for l is also 00100000. int m is determined by k XOR l. This is represented by 00100000 ^ 00100000. Since i and j are the same, and anything XOR itself is the zero, which is different than the other two operators, the result for m is 00000000. Then the outputs are printed as: 32 32 32 32 0 Note that here the values are converted back from binary to decimal.
24th Dec 2017, 6:11 AM
Mohamed
Mohamed - avatar
+ 2
1/2 int i is set to 32. int j is set to the HEX value of 20, which in decimal is 32 (2*16^1+0*16^0). In binary, this is 00100000. That being said, we know i is really 00100000. Things to know are that | is bitwise OR (as opposed to logical), & is bitwise AND (as opposed to logical), and ^ is exclusive or, or XOR. Bitwise means that each value is stored by the computer in it's corresponding binary value as opposed to it's decimal value. Bitwise operations are therefore performed based on the corresponding binary values. OR means you just need 1 of the bits in a certain column to be 1 for the result to be true. For example, 1010 | 1011 is 1011. AND means you need both of the bits in a certain column to be 1 for the result to be true. For example, 1010 | 1011 is 1010. XOR means you need 1 and only 1 of the bits in a certain column to be 1 for the result to be true. For example, 1010 | 1011 is 0001.
24th Dec 2017, 6:05 AM
Mohamed
Mohamed - avatar