15 Bitwise Coding Tricks Every C Programmer Should Know

Bitwise operations are one of the most useful features of the C programming language. They allow programmers to work directly with individual bits, making many operations faster and more efficient than traditional arithmetic methods. Because of this, bit manipulation is widely used in embedded systems, operating systems, device drivers, networking, competitive programming, and coding interviews.

While learning the basic bitwise operators is important, knowing how to apply them to solve real programming problems is even more valuable. Simple tricks such as checking whether a number is a power of two, counting the number of set bits, swapping numbers without a temporary variable, or manipulating individual bits are common interview questions and are frequently used in real-world software development.

In this article, we’ll explore 15 practical bitwise coding tricks that every C programmer should know.

1. Check if a Number is a Power of Two

One of the fastest ways to determine whether a number is a power of two is by using a bitwise AND operation.

A power of two contains only one bit set in its binary representation. Subtracting one from such a number changes that set bit to 0 and all lower bits to 1. Performing an AND operation between the original number and (number - 1) produces zero only for powers of two.

bool isPowerOfTwo(int n)
{
    return (n > 0) && ((n & (n - 1)) == 0);
}

2. Check Whether a Number is Even or Odd

Instead of using the modulo operator, simply inspect the least significant bit.

if (n & 1) 
    { // Odd } 
else
    { // Even }

3. Count the Number of Set Bits

Brian Kernighan’s algorithm removes one set bit during each iteration, making it highly efficient.

while (n) 
{ 
    count++; 
    n &= (n - 1); 
}

4. Check if the Nth Bit is Set

To verify whether a particular bit is set, use a bit mask with the left shift operator.

if (value & (1 << n))
{
    // Bit is set
}

5. Set the Nth Bit

Use the OR operator to set a specific bit without affecting the remaining bits.

value |= (1 << n);

6. Clear the Nth Bit

The AND operator combined with a negated mask clears the selected bit while leaving other bits unchanged.

value &= ~(1 << n);

7. Toggle the Nth Bit

The XOR operator changes the current state of a bit. If it is 1, it becomes 0; if it is 0, it becomes 1.

value ^= (1 << n);

8. Extract the Lowest Set Bit

The following expression isolates the rightmost bit that is set to 1.

lowestBit = n & (-n);

9. Remove the Lowest Set Bit

The expression below clears only the lowest set bit.

n &= (n - 1);

10. Swap Two Numbers Without a Temporary Variable

Two variables can be swapped using the XOR operator.

a ^= b;
b ^= a;
a ^= b;

11. Multiply by Powers of Two

A left shift operation multiplies a number by a power of two.

result = n << 3;

12. Divide by Powers of Two

A right shift operation divides an unsigned integer by a power of two.

result = n >> 2;

13. Find the Position of the Rightmost Set Bit

The lowest set bit can be isolated first, and its position can then be determined.

position = log2(n & -n);

14. Check if Two Numbers Have Opposite Signs

The XOR operator can quickly determine whether two integers have opposite signs.

if ((x ^ y) < 0)
{
    // Opposite signs
}

15. Check Whether the Nth Bit is Clear

Sometimes, instead of checking whether a bit is set, you need to verify whether it is clear (0). This is useful when validating flags, configuration registers, or permission bits before updating them.

if ((value & (1 << n)) == 0)
{
    // Nth bit is clear
}

Conclusion

Bitwise operations are a powerful part of C programming that help solve problems efficiently using simple logic. Whether you’re preparing for coding interviews, improving your programming skills, or working on real-world applications, these bit manipulation tricks can make your code cleaner and more efficient. Practice these techniques regularly, and they’ll become valuable tools in your programming toolkit.

Related Opportunities

Latest Job Notifications