
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.
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)
These specific logic patterns are common “whiteboard” questions because they demonstrate a deep understanding of binary arithmetic.
| Trick | Code | Use Case |
|---|---|---|
| Clear rightmost 1-bit | n & (n-1) | Check power of 2, count set bits |
| Get rightmost 1-bit | n & (-n) | Find least significant set bit |
| Clear rightmost 0-bit | n | (n-1) | Set all bits to the right of rightmost 0 |
| Toggle bits | n ^ mask | Flip bits based on a specific pattern |
n & (n-1)This operation always clears the lowest set bit.
(n > 0) && ((n & (n-1)) == 0), it is a power of 2.int countSetBits(int n) {int count = 0;while (n) {n = n & (n-1); // Clear one bit each timecount++;}return count;}
The XOR operator (^) is unique because a ^ a = 0 and a ^ 0 = a.
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
aandbpoint to the same memory location, as it will clear the value to 0!
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.
For advanced hardware configurations, you often need to manipulate bit fields or check status flags.
// Get n bits starting from position pint getBits(int num, int p, int n) {return ((num >> (p-n+1)) & ~(~0 << n));}// Set n bits starting from position pint setBits(int num, int p, int n, int val) {int mask = (~(~0 << n)) << (p-n+1);return (num & ~mask) | ((val << (p-n+1)) & mask);}
int oppositeSigns(int x, int y) {return ((x ^ y) < 0); // True if different sign bits}
Prepare for these common coding challenges:
(n << d) | (n >> (32 - d)).if statements with masks to find the first set bit.Copy this card into your notes for a quick pre-interview refresh:
| Operation | C Code Snippet |
|---|---|
| Set bit i | n | (1 << i) |
| Clear bit i | n & ~(1 << i) |
| Toggle bit i | n ^ (1 << i) |
| Check bit i | (n >> i) & 1 |
| Clear lowest set bit | n & (n-1) |
| Get lowest set bit | n & (-n) |
| Count set bits | n & (n-1) loop |
| Check power of 2 | n && !(n & (n-1)) |
| Swap a and b | a ^= 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!
Quick Links
Legal Stuff





