0
0
Cprogramming~5 mins

Why bitwise operations are needed in C

Choose your learning style9 modes available
Introduction

Bitwise operations let us work directly with the tiny bits inside numbers. This helps us do tasks faster and use less memory.

When you want to turn on or off specific switches in a device control system.
When you need to pack many small values into one number to save space.
When you want to check if a number is even or odd quickly.
When you want to do fast math like multiplying or dividing by powers of two.
When you work with colors or pixels in images, changing parts of the data efficiently.
Syntax
C
/* Bitwise operations work on integers at the bit level */
int a = 5;       // binary: 00000101
int b = 3;       // binary: 00000011

int and_result = a & b;  // bitwise AND
int or_result = a | b;   // bitwise OR
int xor_result = a ^ b;  // bitwise XOR
int not_result = ~a;     // bitwise NOT
int left_shift = a << 1; // shift bits left by 1
int right_shift = a >> 1;// shift bits right by 1

Bitwise operators work on the binary form of numbers.

They are different from logical operators like && or || which work on true/false values.

Examples
AND operation keeps bits that are 1 in both numbers.
C
int a = 12; // binary 00001100
int b = 10; // binary 00001010
int result = a & b; // result is 8 (00001000)
NOT flips all bits. For positive numbers, result becomes negative.
C
int a = 5; // binary 00000101
int result = ~a; // result is -6 (in two's complement)
Left shift moves bits to the left, multiplying by 2 for each shift.
C
int a = 1; // binary 00000001
int result = a << 3; // result is 8 (00001000)
Right shift moves bits to the right, dividing by 2 for each shift.
C
int a = 16; // binary 00010000
int result = a >> 2; // result is 4 (00000100)
Sample Program

This program shows how to check, turn off, and turn on specific bits using bitwise operations.

C
#include <stdio.h>

int main() {
    int number = 29; // binary 00011101
    int mask = 1 << 3; // binary 00001000

    printf("Original number: %d\n", number);
    
    // Check if 4th bit (bit 3) is set
    if (number & mask) {
        printf("4th bit is ON\n");
    } else {
        printf("4th bit is OFF\n");
    }

    // Turn off the 4th bit
    int new_number = number & (~mask);
    printf("Number after turning off 4th bit: %d\n", new_number);

    // Turn on the 1st bit
    int new_number2 = new_number | 1; // binary OR with 00000001
    printf("Number after turning on 1st bit: %d\n", new_number2);

    return 0;
}
OutputSuccess
Important Notes

Bitwise operations run very fast because they work directly on bits.

They use constant space since they only change bits inside existing numbers.

Common mistake: confusing bitwise operators (&, |) with logical operators (&&, ||).

Use bitwise operations when you need speed and memory efficiency, especially in low-level programming.

Summary

Bitwise operations let you control individual bits inside numbers.

They are useful for fast, low-memory tasks like device control and data packing.

Remember: bitwise operators are different from logical operators.