Bit Operations using Macros

Code

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

// Define bitwise macros here
#define set_bits(reg, pos)  (reg |= (1U << pos))
#define clear_bits(reg, pos)    (reg &= ~(1U << pos))
#define toggle_bits(reg, pos)   (reg ^= (1U << pos))

uint8_t modify_register(uint8_t reg) {
    // Apply operations in order
    set_bits(reg, 2);
    set_bits(reg, 7);
    clear_bits(reg, 3);
    toggle_bits(reg, 5);
    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