Fundamentals 6 min read

Master Bitwise Operations: Essential Tricks for Faster Code

This article introduces the fundamentals of bitwise operations, explains common operators, presents practical tricks such as parity checks, swapping without extra space, clearing and extracting the lowest set bit, and demonstrates real‑world applications like bit counting and permission management with clear C code examples.

Ops Development & AI Practice
Ops Development & AI Practice
Ops Development & AI Practice
Master Bitwise Operations: Essential Tricks for Faster Code

Introduction

Bitwise operations work directly on the binary representation of integers, providing highly efficient data processing for tasks such as counting bits, swapping values, and managing flags.

Basic Concepts

The primary bitwise operators are: & (AND) | (OR) ^ (XOR) ~ (NOT) << (left shift) >> (right shift)

Common Bitwise Tricks

Determine Odd or Even

Use x & 1. The result is 0 for even numbers and 1 for odd numbers.

Swap Two Numbers Without Extra Space

x ^= y;
y ^= x;
x ^= y;

Clear the Lowest Set Bit

Expression x & (x - 1) turns off the least‑significant 1‑bit of x. This is useful for counting set bits because each iteration removes one 1.

Get the Lowest Set Bit

Expression x & -x isolates the least‑significant 1‑bit of x.

Practical Applications

Fast Bit Count

Count the number of 1s in an integer by repeatedly clearing the lowest set bit:

int countBits(int x) {
    int count = 0;
    while (x != 0) {
        x = x & (x - 1); // clear lowest set bit
        count++;
    }
    return count;
}

Permission Management Using Bit Flags

Assign each permission a distinct bit and store the combined permissions in a single integer.

READ = 0x1 // 0001

WRITE = 0x2 // 0010

EXECUTE= 0x4 // 0100

ADMIN = 0x8 // 1000

#include <stdio.h>
#define READ   0x1   // 0001
#define WRITE  0x2   // 0010
#define EXECUTE 0x4  // 0100
#define ADMIN  0x8   // 1000

// Check permission
int has_permission(int permissions, int permission) {
    return permissions & permission;
}

// Set permission
void set_permission(int *permissions, int permission) {
    *permissions |= permission;
}

// Remove permission
void remove_permission(int *permissions, int permission) {
    *permissions &= ~permission;
}

int main() {
    int myPermissions = 0; // no permissions initially
    set_permission(&myPermissions, READ);
    set_permission(&myPermissions, WRITE);
    printf("Permissions after setting Read and Write: %d
", myPermissions);
    if (has_permission(myPermissions, READ)) {
        printf("User has Read permission.
");
    }
    remove_permission(&myPermissions, WRITE);
    printf("Permissions after removing Write: %d
", myPermissions);
    return 0;
}

Analysis of Permission Operations

Set permission : |= turns the target bit on.

Check permission : & tests whether a specific bit is set.

Remove permission : &= combined with ~ clears the target bit.

Conclusion

Bitwise techniques such as the ones described enable fast, compact, and scalable solutions for low‑level tasks like bit counting and flag management. Mastery of these patterns leads to more efficient and maintainable code.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Performance OptimizationC programmingpermission managementbitwise operationsprogramming tricks
Ops Development & AI Practice
Written by

Ops Development & AI Practice

DevSecOps engineer sharing experiences and insights on AI, Web3, and Claude code development. Aims to help solve technical challenges, improve development efficiency, and grow through community interaction. Feel free to comment and discuss.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.