C Bit Manipulation Techniques

Beginner
8 min read
Updated: Nov 2026
2 examples

What Is Bit Manipulation Techniques?

A bitmask is an integer where each bit stands for a yes/no flag. Four patterns do almost everything: set with |=, clear with &= ~mask, toggle with ^=, and test with &. They store many options in one variable.

Remember
#define BIT(n) (1u << (n))
x |=  BIT(3);         /* set bit 3    */
x &= ~BIT(3);         /* clear bit 3  */
x ^=  BIT(3);         /* toggle bit 3 */
if (x & BIT(3)) {...} /* test bit 3   */

Cheat Sheet

Keep these facts handy while you work with Bit Manipulation Techniques in C.

TopicDetail
Setflags |= MASK
Clearflags &= ~MASK
Toggleflags ^= MASK
Power of twon > 0 && (n & (n - 1)) == 0

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Conditional Operator.

Example 1 — Permission flags as a bitmask

Read, write, and execute packed in one unsigned.

C
#include <stdio.h>

#define PERM_READ  (1u << 0)
#define PERM_WRITE (1u << 1)
#define PERM_EXEC  (1u << 2)

int main(void) {
    unsigned perms = 0;

    perms |= PERM_READ | PERM_WRITE;
    printf("perms = %u\n", perms);
    if (perms & PERM_WRITE) printf("can write\n");

    perms &= ~PERM_WRITE;
    printf("after clear = %u\n", perms);

    perms ^= PERM_EXEC;
    printf("after toggle = %u\n", perms);
    return 0;
}

How It Works

  1. READ | WRITE is 001 | 010 = 011 = 3.
  2. perms & PERM_WRITE is non-zero, so the write flag is set.
  3. & ~PERM_WRITE clears bit 1, leaving 1.
  4. ^ PERM_EXEC flips bit 2 on, giving 101 = 5.

Example 2 — Count set bits and detect powers of two

n & (n - 1) clears the lowest set bit.

C
#include <stdio.h>

static int popcount(unsigned n) {
    int count = 0;
    while (n) {
        n &= n - 1;
        count++;
    }
    return count;
}

int main(void) {
    unsigned n = 181;   /* 10110101 */
    printf("bits in %u: %d\n", n, popcount(n));
    printf("64 power of two? %d\n", (64u & 63u) == 0);
    printf("66 power of two? %d\n", (66u & 65u) == 0);
    return 0;
}

How It Works

  1. Each loop pass removes one set bit, so the loop runs once per 1-bit.
  2. 181 is 10110101, which has five 1s.
  3. A power of two has a single set bit, so n & (n - 1) is 0.

Edge Cases & Pitfalls

Check these before relying on Bit Manipulation Techniques in real programs.

1<<

Using int for bit 31

1 << 31 overflows a signed int. Write 1u << 31 or use uint32_t.

~

Forgetting to mask ~

~mask also flips the high bits. That is fine for &= but surprising when you print it.

Key Takeaways

  • Patterns: set |=, clear &= ~, toggle ^=, test &.
  • Constants: name bits with 1u << n.
  • Counting: n & (n - 1) drops the lowest set bit.

One line: A bitmask is an integer where each bit stands for a yes/no flag.

Frequently Asked Questions

A bitmask is an integer constant used with bitwise operators to select, set, clear, or test specific bits in another integer. Each bit usually represents one flag.
AND the value with a mask for that bit: if (value & (1u << n)) is true when bit n is set.
Continue with "C Conditional Operator" next. Choose between two values in one expression with ?:. The C Operators hub lists every topic in order.

Next: Conditional Operator

Choose between two values in one expression with ?:.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful