Interview Questions on Bit Manipulation (C Language)

📘 Basic Bit Manipulation – Short Answer Interview Questions

  1. What are the basic bitwise operators in C?
    AND (&), OR (|), XOR (^), NOT (~), left shift (<<), right shift (>>).
  2. What is the difference between bitwise AND (&) and logical AND (&&)?
    Bitwise AND operates on bits; logical AND operates on boolean values.
  3. How do you check if a number is even or odd using bitwise operations?
    Check LSB: if(num & 1) // odd else // even
  4. How do you set a particular bit in a number?
    Use OR: num |= (1 << position);
  5. How do you clear a particular bit in a number?
    Use AND with NOT: num &= ~(1 << position);
  6. How do you toggle a bit in a number?
    Use XOR: num ^= (1 << position);
  7. How do you check if a bit is set?
    Use AND: if(num & (1 << position)) // bit is set
  8. What is the result of left shifting a number by 1?
    Multiplies the number by 2 (for positive numbers).
  9. What is the result of right shifting a number by 1?
    Divides the number by 2 (for positive numbers).
  10. How do you swap two numbers without a temporary variable?
    Use XOR: a ^= b; b ^= a; a ^= b;
  11. How do you count the number of set bits in a number?
    Brian Kernighan's algorithm: while(n) { count++; n &= n-1; }
  12. What is the difference between arithmetic and logical right shift?
    Arithmetic preserves sign bit; logical always shifts in zeros.
  13. How do you find the most significant set bit?
    Find floor of log2(n) or right shift until number becomes zero.
  14. How do you check if a number is a power of 2?
    Check if (n & (n-1)) == 0 and n != 0.
  15. What is the use of XOR in bit manipulation?
    Toggling bits, finding unique numbers, swapping values, parity checks.
  16. How do you isolate the rightmost set bit?
    Use n & -n or n & ~(n-1).
  17. How do you create a mask for N bits?
    (1 << N) - 1
  18. What is two's complement representation?
    Negative numbers represented as complement of absolute value plus one.
  19. How do you find absolute value without branching?
    Use mask = n >> (sizeof(int)*8 - 1); (n + mask) ^ mask;
  20. How do you reverse the bits of a number?
    Use lookup table or divide and conquer bit swapping.
  21. 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.
  22. How do you find the number that appears once when others appear twice?
    XOR all numbers, duplicates cancel out.
  23. How do you multiply a number by 7 without using * operator?
    (n << 3) - n (equivalent to 8n - n)
  24. How do you divide a number by 8 without using / operator?
    n >> 3
  25. How do you check if two numbers have opposite signs?
    Check sign bit: (num1 ^ num2) < 0
  26. How do you find the next power of 2 for a given number?
    Find MSB and left shift 1 by that position + 1.
  27. What's the difference between bit fields and bitwise operations?
    Bit fields are struct members with specified bit widths; bitwise operations manipulate bits directly.
  28. How do you implement a bit array (bitset)?
    Use array of integers where each bit represents an element.
  29. What is endianness and how does it affect bit manipulation?
    Byte ordering (little/big endian) affects how bytes are stored in memory.
  30. 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

  1. How do you add two numbers without arithmetic operators?
    Use bitwise operations: XOR for sum, AND for carry.
  2. How do you find the maximum of two numbers without comparison?
    Use a ^ ((a ^ b) & -(a < b)) or similar bit trick.
  3. How do you implement a circular shift (rotation) of bits?
    Combine left and right shifts with OR operation.
  4. How do you find the position of the only set bit in a number?
    Check if power of 2, then use log2(n).
  5. How do you count leading zeros in a number?
    Use built-in functions or binary search approach.
  6. How do you count trailing zeros in a number?
    Use built-in functions or find rightmost set bit position.
  7. How do you find the closest number with same number of set bits?
    Find next higher and lower numbers with same popcount.
  8. How do you swap adjacent bits in a number?
    Use masks and shifts: ((n & 0xAAAAAAAA) >> 1) | ((n & 0x55555555) << 1)
  9. How do you find the XOR of all numbers from 1 to N?
    Pattern repeats every 4 numbers: N, 1, N+1, 0.
  10. How do you find the position of the rightmost different bit between two numbers?
    XOR the numbers then find rightmost set bit position.
  11. How do you implement a bit vector (bit array)?
    Use array of integers where each bit represents an element.
  12. How do you find the number of bits to flip to convert A to B?
    Count set bits in A XOR B.
  13. 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.
  14. How do you find the largest power of 2 less than or equal to N?
    Set all bits after MSB to 0.
  15. How do you implement a fast modulo operation using bitwise AND?
    For powers of 2: n & (mod - 1).
  16. How do you check if a number is sparse (no two adjacent bits set)?
    Check if n & (n >> 1) is zero.
  17. How do you find the position of the only different bit between two numbers?
    XOR the numbers then find set bit position.
  18. How do you generate Gray code using bit manipulation?
    n ^ (n >> 1)
  19. How do you find the minimum of two numbers without comparison?
    Use b ^ ((a ^ b) & -(a < b)) or similar bit trick.
  20. What are some real-world applications of bit manipulation?
    Graphics, compression, cryptography, networking protocols, embedded systems.