C Programming Bit Manipulation MCQs
Basic Bit Operations in C (30 Questions)
1. Which operator is used for bitwise AND in C?
A) &&
B) &
C) |
D) ||
Answer: B) &
Single & is the bitwise AND operator, while && is logical AND.
2. What is the result of 5 & 3?
A) 7
B) 1
C) 0
D) 5
Answer: B) 1
5 in binary is 101, 3 is 011. Bitwise AND is 001 which is 1.
3. Which operator is used for bitwise OR?
A) ||
B) &
C) |
D) ^
Answer: C) |
Single | is the bitwise OR operator, while || is logical OR.
4. What is the result of 5 | 3?
A) 7
B) 1
C) 0
D) 5
Answer: A) 7
101 | 011 = 111 which is 7 in decimal.
5. Which operator is used for bitwise XOR?
A) ||
B) &
C) |
D) ^
Answer: D) ^
The caret symbol ^ is used for bitwise XOR operation.
6. What is the result of 5 ^ 3?
A) 7
B) 6
C) 0
D) 5
Answer: B) 6
101 ^ 011 = 110 which is 6 in decimal.
7. Which operator is used for bitwise NOT?
A) ~
B) !
C) -
D) ^
Answer: A) ~
The tilde ~ is used for bitwise NOT operation.
8. What is the result of ~5 assuming 8-bit integers?
A) 250
B) -6
C) 10
D) 5
Answer: B) -6
5 is 00000101, ~5 is 11111010 which is -6 in two's complement.
9. Which operator is used for left shift?
A) <<
B) >>
C) <
D) >
Answer: A) <<
<< is the left shift operator, while >> is right shift.
10. What is the result of 5 << 1?
A) 2
B) 10
C) 3
D) 5
Answer: B) 10
5 (101) shifted left by 1 becomes 1010 which is 10.
11. Which operator is used for right shift?
A) <<
B) >>
C) <
D) >
Answer: B) >>
>> is the right shift operator, while << is left shift.
12. What is the result of 5 >> 1?
A) 2
B) 10
C) 3
D) 5
Answer: A) 2
5 (101) shifted right by 1 becomes 10 which is 2.
13. How do you check if the 3rd bit is set in a number x?
A) x & (1 << 2)
B) x & (1 << 3)
C) x | (1 << 2)
D) x ^ (1 << 2)
Answer: A) x & (1 << 2)
Bits are 0-indexed, so 3rd bit is position 2. This masks all other bits.
14. How do you set the 4th bit in a number x?
A) x & (1 << 3)
B) x | (1 << 3)
C) x ^ (1 << 3)
D) x >> (1 << 3)
Answer: B) x | (1 << 3)
OR operation with a mask sets the bit at position 3 (4th bit).
15. How do you clear the 5th bit in a number x?
A) x & ~(1 << 4)
B) x | ~(1 << 4)
C) x ^ (1 << 4)
D) x >> (1 << 4)
Answer: A) x & ~(1 << 4)
AND with the inverse of the mask clears the bit at position 4 (5th bit).
16. How do you toggle the 6th bit in a number x?
A) x & (1 << 5)
B) x | (1 << 5)
C) x ^ (1 << 5)
D) x >> (1 << 5)
Answer: C) x ^ (1 << 5)
XOR with a mask toggles the bit at position 5 (6th bit).
17. What is the result of (x & (x-1))?
A) Clears the least significant set bit
B) Sets the least significant zero bit
C) Toggles all bits
D) Reverses the bits
Answer: A) Clears the least significant set bit
This is a common bit trick to clear the rightmost set bit.
18. What does (x & -x) give you?
A) The least significant set bit
B) The most significant set bit
C) All set bits
D) The number of set bits
Answer: A) The least significant set bit
This isolates the rightmost set bit in x.
19. How do you check if a number is a power of 2?
A) (x & (x-1)) == 0
B) (x | (x-1)) == 0
C) (x ^ (x-1)) == 0
D) (x >> 1) == 0
Answer: A) (x & (x-1)) == 0
Powers of 2 have exactly one bit set, so x & (x-1) clears it.
20. How do you count the number of set bits in a number?
A) Using a loop with x >>= 1
B) Using x & (x-1) in a loop
C) Using built-in __builtin_popcount
D) All of the above
Answer: D) All of the above
All are valid methods, with varying efficiency.
21. What is the result of ~0?
A) 0
B) 1
C) -1
D) Undefined
Answer: C) -1
~0 flips all bits to 1, which is -1 in two's complement.
22. How do you swap two numbers without a temporary variable?
A) a ^= b; b ^= a; a ^= b;
B) a = b; b = a;
C) a += b; b = a - b; a -= b;
D) Both A and C
Answer: D) Both A and C
Both XOR and arithmetic methods work for swapping.
23. What is the result of 1 << 0?
A) 0
B) 1
C) 2
D) Undefined
Answer: B) 1
Shifting left by 0 leaves the number unchanged.
24. How do you create a mask with n bits set?
A) (1 << n) - 1
B) (1 << (n-1)) - 1
C) ~0 << n
D) ~0 >> n
Answer: A) (1 << n) - 1
This creates a mask with the rightmost n bits set.
25. What is the result of 0x55 & 0xAA?
A) 0x00
B) 0xFF
C) 0x55
D) 0xAA
Answer: A) 0x00
0x55 is 01010101, 0xAA is 10101010 - AND gives all zeros.
26. What is the result of 0x55 | 0xAA?
A) 0x00
B) 0xFF
C) 0x55
D) 0xAA
Answer: B) 0xFF
OR of these complementary patterns gives all ones (0xFF).
27. What is the result of 0x55 ^ 0xAA?
A) 0x00
B) 0xFF
C) 0x55
D) 0xAA
Answer: B) 0xFF
XOR of complementary patterns gives all ones (0xFF).
28. How do you determine if a number is odd using bitwise operations?
A) x & 0
B) x & 1
C) x | 1
D) x ^ 1
Answer: B) x & 1
Odd numbers have the least significant bit set.
29. How do you multiply a number by 2 using bit operations?
A) x << 1
B) x >> 1
C) x & 2
D) x | 2
Answer: A) x << 1
Left shift by 1 multiplies by 2 (for positive numbers).
30. How do you divide a number by 2 using bit operations?
A) x << 1
B) x >> 1
C) x & 2
D) x | 2
Answer: B) x >> 1
Right shift by 1 divides by 2 (integer division for positive numbers).
Advanced Bit Manipulation in C (20 Questions)
1. How do you find the position of the most significant set bit?
A) Using log2(x)
B) Using a loop with x >>= 1
C) Using __builtin_clz()
D) All of the above
Answer: D) All of the above
All these methods can find the position of the MSB.
2. How do you check if two numbers have opposite signs?
A) (x ^ y) < 0
B) (x & y) < 0
C) (x | y) < 0
D) (x - y) < 0
Answer: A) (x ^ y) < 0
XOR of two numbers with opposite signs will have the sign bit set.
3. How do you compute the absolute value of an integer without branching?
A) (x + (x >> 31)) ^ (x >> 31)
B) x < 0 ? -x : x
C) abs(x)
D) x & ~(1 << 31)
Answer: A) (x + (x >> 31)) ^ (x >> 31)
This bit trick avoids conditional branches for absolute value.
4. How do you swap the values of two bits at positions i and j in a number?
A) x ^= ((x >> i) ^ (x >> j)) & 1
B) x ^= ((1 << i) | (1 << j))
C) if (((x >> i) & 1) != ((x >> j) & 1)) { x ^= (1 << i) | (1 << j); }
D) Both A and C
Answer: D) Both A and C
Both methods correctly swap bits when they differ.
5. How do you reverse the bits in a byte?
A) Using a lookup table
B) Using bit operations in a loop
C) Using __builtin_bitreverse8()
D) All of the above
Answer: D) All of the above
All are valid methods for reversing bits in a byte.
6. How do you find the next power of 2 for a given number?
A) 1 << (floor(log2(x)) + 1)
B) x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; x++;
C) Using __builtin_clz()
D) All of the above
Answer: D) All of the above
All these methods can find the next power of 2.
7. How do you compute the parity of a number (count of 1s modulo 2)?
A) Using a loop with x &= x - 1
B) Using XOR operations in a divide-and-conquer approach
C) Using __builtin_parity()
D) All of the above
Answer: D) All of the above
All are valid methods for computing parity.
8. How do you round up a number to the next multiple of a power of 2?
A) (x + (1 << n) - 1) & ~((1 << n) - 1)
B) ((x - 1) | ((1 << n) - 1)) + 1
C) x + (-x & ((1 << n) - 1))
D) All of the above
Answer: D) All of the above
All these bit tricks correctly round up to the next multiple.
9. How do you find the rightmost different bit between two numbers?
A) (x ^ y) & -(x ^ y)
B) __builtin_ctz(x ^ y)
C) log2((x ^ y) & -(x ^ y))
D) All of the above
Answer: D) All of the above
All these methods find the rightmost differing bit position.
10. How do you rotate a number left by n bits?
A) (x << n) | (x >> (sizeof(x)*8 - n))
B) (x >> n) | (x << (sizeof(x)*8 - n))
C) __builtin_rotateleft(x, n)
D) Both A and C
Answer: D) Both A and C
The bit operation and built-in both perform left rotation.
11. How do you rotate a number right by n bits?
A) (x << n) | (x >> (sizeof(x)*8 - n))
B) (x >> n) | (x << (sizeof(x)*8 - n))
C) __builtin_rotateright(x, n)
D) Both B and C
Answer: D) Both B and C
The bit operation and built-in both perform right rotation.
12. How do you set all bits to the right of the most significant set bit?
A) x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16;
B) (1 << (int)(log2(x) + 1)) - 1
C) ~0 << (__builtin_clz(x) + 1)
D) All of the above
Answer: D) All of the above
All these methods create a mask with all bits set below the MSB.
13. How do you check if a number is a power of 4?
A) (x & (x-1)) == 0 && (x & 0x55555555) != 0
B) (x > 0) && ((x & (x - 1)) == 0) && ((x & 0xAAAAAAAA) == 0)
C) __builtin_popcount(x) == 1 && __builtin_ctz(x) % 2 == 0
D) All of the above
Answer: D) All of the above
All these methods correctly identify powers of 4.
14. How do you find the position of the only set bit in a power of 2?
A) log2(x)
B) __builtin_ctz(x)
C) __builtin_ffs(x) - 1
D) All of the above
Answer: D) All of the above
All these methods find the position of the single set bit.
15. How do you swap adjacent bits in a number?
A) ((x & 0xAAAAAAAA) >> 1) | ((x & 0x55555555) << 1)
B) ((x >> 1) & 0x55555555) | ((x << 1) & 0xAAAAAAAA)
C) __builtin_bswap32(x) >> 16
D) Both A and B
Answer: D) Both A and B
Both methods correctly swap adjacent bits.
16. How do you reverse the bits in a 32-bit integer?
A) Using a lookup table for each byte
B) Using divide and conquer bit operations
C) Using __builtin_bitreverse32()
D) All of the above
Answer: D) All of the above
All are valid methods for reversing 32-bit integers.
17. How do you count trailing zeros in a number?
A) __builtin_ctz(x)
B) log2(x & -x)
C) Using a loop with x >>= 1
D) All of the above
Answer: D) All of the above
All these methods count trailing zeros.
18. How do you create a mask that covers the least significant n bits?
A) (1 << n) - 1
B) ~(~0 << n)
C) 0xFFFFFFFF >> (32 - n)
D) All of the above
Answer: D) All of the above
All these expressions create a mask with n least significant bits set.
19. How do you find the number of bits needed to represent a number?
A) floor(log2(x)) + 1
B) 32 - __builtin_clz(x)
C) Using a loop with x >>= 1
D) All of the above
Answer: D) All of the above
All these methods calculate the number of bits needed.
20. How do you implement a bit array in C?
A) Using an array of integers
B) Using an array of characters
C) Using the bitset header
D) All of the above
Answer: D) All of the above
All are valid approaches to implement a bit array in C.
Related Bit Manipulation Resources