0
0
Cprogramming~5 mins

Bit manipulation techniques

Choose your learning style9 modes available
Introduction
Bit manipulation lets you work directly with the tiny bits inside numbers. It helps you do tasks faster and use less memory.
You want to check if a number is even or odd quickly.
You need to turn on or off specific features represented by bits in a setting.
You want to swap two numbers without extra space.
You want to count how many bits are set to 1 in a number.
You want to store multiple true/false values compactly.
Syntax
C
/* Bit manipulation operations in C */

// Set a bit: number |= (1U << position);
// Clear a bit: number &= ~(1U << position);
// Toggle a bit: number ^= (1U << position);
// Check a bit: (number & (1U << position)) != 0

// Example function to set a bit:
unsigned int set_bit(unsigned int number, int position) {
    return number | (1U << position);
}
Bits are counted from 0 starting at the rightmost bit (least significant bit).
Use unsigned types to avoid unexpected behavior with shifts.
Examples
This example shows setting, clearing, toggling, and checking bits on a small number.
C
unsigned int number = 0b00001010; // 10 in decimal
// Set bit at position 1
number |= (1U << 1); // number becomes 0b00001010 (bit 1 already set)

// Clear bit at position 3
number &= ~(1U << 3); // number becomes 0b00000010

// Toggle bit at position 0
number ^= (1U << 0); // number becomes 0b00000011

// Check bit at position 2
int bit_is_set = (number & (1U << 2)) != 0; // bit_is_set is 0
Shows setting bits on an empty number, including the highest bit.
C
unsigned int number = 0; // all bits off
// Set bit at position 0
number |= (1U << 0); // number is 1

// Set bit at position 31 (highest bit in 32-bit)
number |= (1U << 31); // number has highest bit set
Shows clearing and toggling bits on a number with all bits set.
C
unsigned int number = 0xFFFFFFFF; // all bits on
// Clear bit at position 15
number &= ~(1U << 15); // bit 15 cleared

// Toggle bit at position 15
number ^= (1U << 15); // bit 15 set again
Sample Program
This program shows how to set, clear, toggle, and check bits in a number. It prints the bits before and after each operation for clear understanding.
C
#include <stdio.h>

// Function to print bits of a 32-bit number
void print_bits(unsigned int number) {
    for (int i = 31; i >= 0; i--) {
        unsigned int bit = (number >> i) & 1U;
        printf("%u", bit);
        if (i % 8 == 0 && i != 0) {
            printf(" "); // space every byte for readability
        }
    }
    printf("\n");
}

int main() {
    unsigned int number = 0b00000000000000000000000000001010; // 10 decimal
    printf("Original number bits:   ");
    print_bits(number);

    // Set bit at position 4
    number |= (1U << 4);
    printf("After setting bit 4:    ");
    print_bits(number);

    // Clear bit at position 1
    number &= ~(1U << 1);
    printf("After clearing bit 1:   ");
    print_bits(number);

    // Toggle bit at position 3
    number ^= (1U << 3);
    printf("After toggling bit 3:  ");
    print_bits(number);

    // Check bit at position 2
    int bit2 = (number & (1U << 2)) != 0;
    printf("Bit at position 2 is:  %d\n", bit2);

    return 0;
}
OutputSuccess
Important Notes
Bit manipulation operations run very fast because they work directly on bits.
Time complexity for these operations is O(1) because they do a fixed number of steps.
Be careful with signed integers and shifting; use unsigned types to avoid surprises.
Use bit manipulation when you want to save memory or speed up low-level tasks.
Summary
Bit manipulation lets you change or check individual bits inside numbers.
Common operations are setting, clearing, toggling, and checking bits.
Use unsigned integers and bitwise operators like |, &, ^, and << for these tasks.