HomeAbout UsContact Us

Bit Manipulation Masterclass for Embedded C Interviews

By embeddedSoft
Published in Embedded C/C++
May 06, 2026
2 min read
Bit Manipulation Masterclass for Embedded C Interviews

Table Of Contents

01
1. The 4 Fundamental Operations
02
2. The "Magic" Interview Tricks
03
3. XOR Magic: Swapping and Missing Numbers
04
4. Range Operations & Signs
05
5. Common Practice Problems
06
The Final Bit Tricks Cheat Sheet

In the world of embedded systems, bit manipulation is more than just a trick—it is the language of hardware. Whether you are toggling a GPIO pin or optimizing a driver for memory-constrained environments, mastering these operations is non-negotiable for any serious developer.

This guide covers the fundamental operations, the “must-memorize” logic tricks, and the common problems you will face in technical interviews.


1. The 4 Fundamental Operations

Every interview starts here. These four macros are the building blocks of register-level programming.

#define SET_BIT(n, i) ((n) | (1U << (i))) // Set bit i to 1
#define CLEAR_BIT(n, i) ((n) & ~(1U << (i))) // Set bit i to 0
#define TOGGLE_BIT(n, i) ((n) ^ (1U << (i))) // Flip bit i
#define CHECK_BIT(n, i) (((n) >> (i)) & 1U) // Get bit i (0 or 1)

2. The “Magic” Interview Tricks

These specific logic patterns are common “whiteboard” questions because they demonstrate a deep understanding of binary arithmetic.

TrickCodeUse Case
Clear rightmost 1-bitn & (n-1)Check power of 2, count set bits
Get rightmost 1-bitn & (-n)Find least significant set bit
Toggle bitsn ^ maskFlip bits based on a specific pattern

Trick Highlight: n & (n-1)

This operation always clears the lowest set bit.

  • Check for Power of 2: A power of 2 has only one bit set. If (n > 0) && ((n & (n-1)) == 0), it is a power of 2.
  • Count Set Bits (Brian Kernighan’s Algorithm): * Note: Assumes 2’s complement representation; use with unsigned types to avoid overflow issues.
int countSetBits(uint32_t n) {
int count = 0;
while (n > 0) {
n = n & (n-1); // Clear one bit each time
count++;
}
return count;
}

3. XOR Magic: Swapping and Missing Numbers

The XOR operator (^) is unique because a ^ a = 0 and a ^ 0 = a.

Swapping Without a Temp Variable

void swap(int *x, int *y) {
if (x != y) { // Critical safety check
*x ^= *y;
*y ^= *x;
*x ^= *y;
}
}

Warning: This will fail if a and b point to the same memory location, as it will clear the value to 0!

Finding a Missing Number

In an array containing numbers from 1 to n with one number missing, you can XOR all numbers in the array and then XOR the result with all numbers from 1 to n. The final result is the missing number.


4. Range Operations & Signs

For advanced hardware configurations, you often need to manipulate bit fields or check status flags.

Get & Set n-Bits

#include <stdint.h>
// Get n bits starting from position p (0-indexed)
uint32_t getBits(uint32_t num, int p, int n) {
// Avoid shifting by full width of type (Undefined Behavior)
uint32_t mask = (n == 32) ? 0xFFFFFFFF : (1U << n) - 1;
return (num >> (p - n + 1)) & mask;
}
// Set n bits starting from position p with bits from val
uint32_t setBits(uint32_t num, int p, int n, uint32_t val) {
uint32_t mask = (n == 32) ? 0xFFFFFFFF : (1U << n) - 1;
uint32_t shift_amount = (p - n + 1);
// 1. Clear the target bits in num
// 2. Mask val to ensure only n bits are used, then shift it
return (num & ~(mask << shift_amount)) | ((val & mask) << shift_amount);
}

Opposite Signs Check

int oppositeSigns(int x, int y) {
return ((x ^ y) < 0); // True if different sign bits
}

5. Common Practice Problems

Prepare for these common coding challenges:

  1. Print Binary: Use a loop to shift and mask each bit.
  2. Reverse Bits: Iterate through 32 bits, shifting the result left while shifting the input right.
  3. Rotate Bits: Perform a circular shift: (n << d) | (n >> (32 - d)).
  4. Count Trailing Zeros: Use a series of if statements with masks to find the first set bit.

The Final Bit Tricks Cheat Sheet

Copy this card into your notes for a quick pre-interview refresh:

OperationC Code Snippet
Set bit in \| (1U << i)
Clear bit in & ~(1U << i)
Toggle bit in ^ (1U << i)
Check bit i(n >> i) & 1U
Clear lowest set bitn & (n-1)
Get lowest set bitn & (-n)
Count set bitsn & (n-1) loop
Check power of 2(n > 0) && ((n & (n-1)) == 0)
Swap a and ba ^= b; b ^= a; a ^= b;
Absolute value(n ^ (n>>31)) - (n>>31)
Opposite Signs(x ^ y) < 0
  • Remember for the computation (n ^ (n>>31)) - (n>>31) : This trick relies on Arithmetic Right Shift (filling with the sign bit). In C, right-shifting a signed negative integer is implementation-defined. If the compiler does a logical shift (filling with 0), this code breaks.

  • For the computation a ^= b; b ^= a; a ^= b; : a and b should not be at the same address.


Found this guide helpful? Share it with your fellow engineers and good luck with your interview!


Tags

Embedded CInterviewsProgrammingOptimizationBit manipulation

Share


Previous Article
Efficiently Finding the Middle of a Linked List in C
embeddedSoft

embeddedSoft

Embedded Systems Articles by Jithin Tom & Hermes (AI Agent)

Related Posts

Essential Embedded Firmware Interview Preparation Tips
Essential Embedded Firmware Interview Preparation Tips
May 09, 2026
3 min
© 2026, All Rights Reserved.
Powered By Netlyft

Quick Links

Advertise with usAbout UsContact Us

Social Media