Here is a listing of C questions on “Bitwise Operators” along with answers, explanations and/or solutions:
1. What will be the output of the following C code?
-
#include
-
void main()
-
{
-
int a = 5, b = -7, c = 0, d;
-
d = ++a && ++b || ++c;
-
printf("n%d%d%d%d", a, b, c, d);
-
}
a) 6 -6 0 0
b) 6 -5 0 1
c) -6 -6 0 1
d) 6 -6 0 1
Answer: d
Clarification: None.
2. What will be the output of the following C code?
-
#include
-
void main()
-
{
-
int a = -5;
-
int k = (a++, ++a);
-
printf("%dn", k);
-
}
a) -3
b) -5
c) 4
d) Undefined
Answer: a
Clarification: None.
3. What will be the output of the following C code?
-
#include
-
int main()
-
{
-
int x = 2;
-
x = x << 1;
-
printf("%dn", x);
-
}
a) 4
b) 1
c) Depends on the compiler
d) Depends on the endianness of the machine
Answer: a
Clarification: None.
4. What will be the output of the following C code?
-
#include
-
int main()
-
{
-
int x = -2;
-
x = x >> 1;
-
printf("%dn", x);
-
}
a) 1
b) -1
c) 2 31 – 1 considering int to be 4 bytes
d) Either -1 or 1
Answer: b
Clarification: None.
5. What will be the output of the following C code?
-
#include
-
int main()
-
{
-
if (~0 == 1)
-
printf("yesn");
-
else
-
printf("non");
-
}
a) yes
b) no
c) compile time error
d) undefined
Answer: b
Clarification: None.
6. What will be the output of the following C code?
-
#include
-
int main()
-
{
-
int x = -2;
-
if (!0 == 1)
-
printf("yesn");
-
else
-
printf("non");
-
}
a) yes
b) no
c) run time error
d) undefined
Answer: a
Clarification: None.
7. What will be the output of the following C code?
-
#include
-
int main()
-
{
-
int y = 0;
-
if (1 |(y = 1))
-
printf("y is %dn", y);
-
else
-
printf("%dn", y);
-
-
}
a) y is 1
b) 1
c) run time error
d) undefined
Answer: a
Clarification: None.
8. What will be the output of the following C code?
-
#include
-
int main()
-
{
-
int y = 1;
-
if (y & (y = 2))
-
printf("true %dn", y);
-
else
-
printf("false %dn", y);
-
-
}
a) true 2
b) false 2
c) either true 2 or false 2
d) true 1
Answer: a
Clarification: None.