HomeAbout UsContact Us

Bit Manipulation Masterclass for Embedded C Interviews

By embeddedSoft
Published in Embedded C/C++
May 06, 2026
1 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) | (1 << i)) // Set bit i to 1
#define CLEAR_BIT(n, i) ((n) & ~(1 << i)) // Set bit i to 0
#define TOGGLE_BIT(n, i) ((n) ^ (1 << i)) // Flip bit i
#define CHECK_BIT(n, i) (((n) >> i) & 1) // 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
Clear rightmost 0-bitn | (n-1)Set all bits to the right of rightmost 0
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):
int countSetBits(int n) {
int count = 0;
while (n) {
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 *a, int *b) {
*a = *a ^ *b;
*b = *a ^ *b; // (a^b)^b = a
*a = *a ^ *b; // (a^b)^a = b
}

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

// Get n bits starting from position p
int getBits(int num, int p, int n) {
return ((num >> (p-n+1)) & ~(~0 << n));
}
// Set n bits starting from position p
int setBits(int num, int p, int n, int val) {
int mask = (~(~0 << n)) << (p-n+1);
return (num & ~mask) | ((val << (p-n+1)) & mask);
}

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 | (1 << i)
Clear bit in & ~(1 << i)
Toggle bit in ^ (1 << i)
Check bit i(n >> i) & 1
Clear lowest set bitn & (n-1)
Get lowest set bitn & (-n)
Count set bitsn & (n-1) loop
Check power of 2n && !(n & (n-1))
Swap a and ba ^= b; b ^= a; a ^= b;
Absolute value(n ^ (n>>31)) - (n>>31)
Opposite Signs(x ^ y) < 0

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

Insightful articles on embedded systems

Related Posts

Efficiently Finding the Middle of a Linked List in C
Efficiently Finding the Middle of a Linked List in C
May 06, 2026
1 min
© 2026, All Rights Reserved.
Powered By Netlyft

Quick Links

Advertise with usAbout UsContact Us

Social Media