Interview Questions on Bit Manipulation (C Language)
📘 Basic Bit Manipulation – Short Answer Interview Questions
-
What are the basic bitwise operators in C?
AND (&), OR (|), XOR (^), NOT (~), left shift (<<), right shift (>>).
-
What is the difference between bitwise AND (&) and logical AND (&&)?
Bitwise AND operates on bits; logical AND operates on boolean values.
-
How do you check if a number is even or odd using bitwise operations?
Check LSB:
if(num & 1) // odd else // even -
How do you set a particular bit in a number?
Use OR:
num |= (1 << position); -
How do you clear a particular bit in a number?
Use AND with NOT:
num &= ~(1 << position); -
How do you toggle a bit in a number?
Use XOR:
num ^= (1 << position); -
How do you check if a bit is set?
Use AND:
if(num & (1 << position)) // bit is set -
What is the result of left shifting a number by 1?
Multiplies the number by 2 (for positive numbers).
-
What is the result of right shifting a number by 1?
Divides the number by 2 (for positive numbers).
-
How do you swap two numbers without a temporary variable?
Use XOR:
a ^= b; b ^= a; a ^= b; -
How do you count the number of set bits in a number?
Brian Kernighan's algorithm:
while(n) { count++; n &= n-1; } -
What is the difference between arithmetic and logical right shift?
Arithmetic preserves sign bit; logical always shifts in zeros.
-
How do you find the most significant set bit?
Find floor of log2(n) or right shift until number becomes zero.
-
How do you check if a number is a power of 2?
Check if
(n & (n-1)) == 0andn != 0. -
What is the use of XOR in bit manipulation?
Toggling bits, finding unique numbers, swapping values, parity checks.
-
How do you isolate the rightmost set bit?
Use
n & -norn & ~(n-1). -
How do you create a mask for N bits?
(1 << N) - 1 -
What is two's complement representation?
Negative numbers represented as complement of absolute value plus one.
-
How do you find absolute value without branching?
Use
mask = n >> (sizeof(int)*8 - 1); (n + mask) ^ mask; -
How do you reverse the bits of a number?
Use lookup table or divide and conquer bit swapping.
-
How do you find the missing number in an array of numbers from 1 to N?
XOR all numbers with 1 to N, result is missing number.
-
How do you find the number that appears once when others appear twice?
XOR all numbers, duplicates cancel out.
-
How do you multiply a number by 7 without using * operator?
(n << 3) - n(equivalent to 8n - n) -
How do you divide a number by 8 without using / operator?
n >> 3 -
How do you check if two numbers have opposite signs?
Check sign bit:
(num1 ^ num2) < 0 -
How do you find the next power of 2 for a given number?
Find MSB and left shift 1 by that position + 1.
-
What's the difference between bit fields and bitwise operations?
Bit fields are struct members with specified bit widths; bitwise operations manipulate bits directly.
-
How do you implement a bit array (bitset)?
Use array of integers where each bit represents an element.
-
What is endianness and how does it affect bit manipulation?
Byte ordering (little/big endian) affects how bytes are stored in memory.
-
How do you find the parity of a number (odd/even number of set bits)?
XOR all bits together or use lookup table.
📗 Advanced Bit Manipulation – Short Answer Interview Questions
-
How do you add two numbers without arithmetic operators?
Use bitwise operations: XOR for sum, AND for carry.
-
How do you find the maximum of two numbers without comparison?
Use
a ^ ((a ^ b) & -(a < b))or similar bit trick. -
How do you implement a circular shift (rotation) of bits?
Combine left and right shifts with OR operation.
-
How do you find the position of the only set bit in a number?
Check if power of 2, then use log2(n).
-
How do you count leading zeros in a number?
Use built-in functions or binary search approach.
-
How do you count trailing zeros in a number?
Use built-in functions or find rightmost set bit position.
-
How do you find the closest number with same number of set bits?
Find next higher and lower numbers with same popcount.
-
How do you swap adjacent bits in a number?
Use masks and shifts:
((n & 0xAAAAAAAA) >> 1) | ((n & 0x55555555) << 1) -
How do you find the XOR of all numbers from 1 to N?
Pattern repeats every 4 numbers: N, 1, N+1, 0.
-
How do you find the position of the rightmost different bit between two numbers?
XOR the numbers then find rightmost set bit position.
-
How do you implement a bit vector (bit array)?
Use array of integers where each bit represents an element.
-
How do you find the number of bits to flip to convert A to B?
Count set bits in A XOR B.
-
How do you generate all subsets of a set using bit manipulation?
Use numbers from 0 to 2^n-1 where each bit represents element inclusion.
-
How do you find the largest power of 2 less than or equal to N?
Set all bits after MSB to 0.
-
How do you implement a fast modulo operation using bitwise AND?
For powers of 2:
n & (mod - 1). -
How do you check if a number is sparse (no two adjacent bits set)?
Check if
n & (n >> 1)is zero. -
How do you find the position of the only different bit between two numbers?
XOR the numbers then find set bit position.
-
How do you generate Gray code using bit manipulation?
n ^ (n >> 1) -
How do you find the minimum of two numbers without comparison?
Use
b ^ ((a ^ b) & -(a < b))or similar bit trick. -
What are some real-world applications of bit manipulation?
Graphics, compression, cryptography, networking protocols, embedded systems.
Related Bit Manipulation Resources