Bit Operations using Macros

Code

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

// Define bitwise macros here
#define BITS_2_AND_7 (0x84U)
#define BIT_3 (0x08U)
#define BIT_5 (0x20U)

#define SET_2_AND_7(x) ((x) |= (BITS_2_AND_7))
#define CLEAR_3(x) ((x) &= ~(BIT_3))
#define TOGGLE_5(x) ((x) ^= (BIT_5)) 


uint8_t modify_register(uint8_t reg) {
    SET_2_AND_7(reg);
    CLEAR_3(reg);
    TOGGLE_5(reg);
    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