Bit Operations using Macros

Code

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

// Define bitwise macros here
//Trong các SDK của STM32, AVR, hay Linux Kernel, 
//"Bitwise Macros" thường ám chỉ các Macro Function tái sử dụng được, 
//ví dụ: SET_BIT(reg, bit), CLEAR_BIT(reg, bit)
#define BIT_SET ((1U << 2) | (1U << 7))
#define BIT_CLEAR (1U << 3)
#define BIT_TOGGLE (1U << 5)

#define SET_BIT(REG, BIT) (REG |= (BIT))
#define CLEAR_BIT(REG, BIT) (REG &= ~(BIT))
#define TOGGLE_BIT(REG, BIT) (REG ^= (BIT))


uint8_t modify_register(uint8_t reg) {
    // Apply operations in order
    SET_BIT(reg, BIT_SET);
    CLEAR_BIT(reg, BIT_CLEAR);
    TOGGLE_BIT(reg, BIT_TOGGLE);
    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