All submissions

Bit Operations using Macros

Code

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

// Define bitwise macros here
#define set_bit(reg, pos)     ((reg) | (1U << (pos)))
#define clear_bit(reg, pos)   ((reg) & ~(1U << (pos)))
#define toggle_bit(reg, pos)  ((reg) ^ (1U << (pos)))

uint8_t modify_register(uint8_t reg) {
    // Apply operations in order
    reg = set_bit(reg, 2);
    reg = set_bit(reg, 7);
    reg = clear_bit(reg, 3);
    reg = toggle_bit(reg, 5);
    return reg;
}

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

Solving Approach

In order to keep the other bits as-is while modifying specific bits, we use bitwise operators and bit masks which will use left shift operator (<<):
 OR (|) for setting a bit

AND (&) with complement (~) of a mask to clear a bit

XOR (^) to toggle a bit

 

 

 

Loading...

Input

0

Expected Output

164