0% found this document useful (0 votes)
12 views1 page

C Programming Test on Bitwise Operators

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views1 page

C Programming Test on Bitwise Operators

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C Programming Test: Bitwise Operators

Section A — Multiple Choice Questions (1 mark each)

1. What is the output of the following code?


int a = 5, b = 9; printf("%d", a & b); a) 1 b) 5 c) 9 d) 0

2. What is the result of 12 | 25 in binary?


a) 29 b) 25 c) 13 d) 37

3. What will ~5 evaluate to (on a 32-bit system)?


a) 5 b) -6 c) 6 d) -5

4. Which operator is used to perform a bitwise XOR in C?


a) && b) || c) ^ d) ~

5. What is the result of the expression 8 >> 1?


a) 16 b) 4 c) 2 d) 1

Section B — Output Prediction (2 marks each)

6. Predict the output:


int a = 7; int b = a << 2; printf("%d", b);
7. Predict the output:
int x = 10, y = 20; x = x ^ y; y = x ^ y; x = x ^ y; printf("%d %d", x, y);
8. What will this print?
int a = 12; printf("%d", a & (~a));

Section C — Debugging (3 marks each)

9. Find and correct the error(s).


int a = 5, b = 2; if (a && b) printf("%d", a|b); (Hint: The operator might not be what was intended.)

10. Find and correct the error(s).


int a = 4; printf("%d", a >>> 1); (Hint: Check if the operator exists in C.)

Section D — Short Coding (5 marks each)

11. Write a C program to check if a given number is even or odd using bitwise operators only. (Hint:
Use n & 1.)

12. Write a function countSetBits(int n) that returns the number of 1’s in the binary representation of
n.

Section E — Challenge Question (7 marks)

13. Without using arithmetic operators, write a C function to find the sum of two integers using
bitwise operators. (Hint: Use ^ for sum and & for carry.)

You might also like