Bit Operations using Macros

Code

/* This code is to perform bit operations using macros
   submitted by Manish Gowda T
   accomplished by leftshift and AND,OR,XOR and macros function*/

#include <stdio.h>
#include <stdint.h>

// Define bitwise macros here

#define setbit(reg,pos)(reg | (1<<pos))
#define clearbit(reg,pos)(reg &(~(1<<pos)))
#define togglebit(reg,pos)(reg ^ (1<<pos))

uint8_t modify_register(uint8_t reg) {
   
   // helps to send values to macros
    reg =setbit(reg,2);
    reg=setbit(reg,7);
    reg=clearbit(reg,3);
    reg=togglebit(reg,5);
    return reg;
}

int main() {
    uint8_t reg;
    scanf("%hhu", &reg);
    printf("%u", modify_register(reg));
    return 0;
}

Solving Approach

i have used macros here 
it is also done by left shift and bitwise operators

 

 

Upvote
Downvote
Loading...

Expected Output

164