Bit Operations using Macros

Code

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

// Define bitwise macros here
#define SETBIT(x, reg) (reg |= (1U << x))
#define CLEARBIT(x, reg) (reg &= (~(1U << x)))
#define TOGGLEBIT(x, reg) (reg ^= (1U << x))

uint8_t modify_register(uint8_t reg) {
    // Apply operations in order

    SETBIT(2, reg);
    SETBIT(7, reg);

    CLEARBIT(3, reg);

    TOGGLEBIT(5, reg);

    return reg;
}

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

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

0

Expected Output

164