Bit Manipulation

 Bit Manipulation


Bit manipulation involves performing operations on individual bits within a data structure. It allows for compact and efficient storage of data and is commonly used in low-level programming and embedded systems.



#include <stdio.h>


#define SET_BIT(x, pos) (x |= (1 << pos))

#define CLEAR_BIT(x, pos) (x &= ~(1 << pos))

#define TOGGLE_BIT(x, pos) (x ^= (1 << pos))

#define CHECK_BIT(x, pos) (x & (1 << pos))


int main() {

    unsigned char flags = 0x00;


    SET_BIT(flags, 1); // Set bit at position 1

    printf("Bit at position 1: %d\n", CHECK_BIT(flags, 1));


    TOGGLE_BIT(flags, 2); // Toggle bit at position 2

    printf("Bit at position 2: %d\n", CHECK_BIT(flags, 2));


    CLEAR_BIT(flags, 0); // Clear bit at position 0

    printf("Bit at position 0: %d\n", CHECK_BIT(flags, 0));


    return 0;

}

No comments:

Post a Comment

The Importance of Cybersecurity in the Digital Age

 The Importance of Cybersecurity in the Digital Age Introduction: In today's digital age, where technology is deeply intertwined with ev...