Bit Operations using Macros

Code

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

// Define bitwise macros here
#define BIT(n) ((uint8_t)(1u << (n)))
#define SET_MASK (BIT(2) | BIT(7))
#define CLEAR_MASK BIT(3)
#define TOGGLE_MASK BIT(5)
uint8_t modify_register(uint8_t reg) {
    // Apply operations in order
    reg |= SET_MASK;
    reg &= (uint8_t)(~CLEAR_MASK);
    reg ^= TOGGLE_MASK;
    return reg;
}

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

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Expected Output

164