Bit Operations using Macros

Code

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

#define BIT(n)              (1u << (n))

#define SET_BITS_MASK       (BIT(2) | BIT(7))
#define CLEAR_BITS_MASK     BIT(3)
#define TOGGLE_BITS_MASK    BIT(5)

uint8_t modify_register(uint8_t reg)
{
    reg |= SET_BITS_MASK;              // Set bits 2 and 7
    reg &= (uint8_t)~CLEAR_BITS_MASK;  // Clear bit 3
    reg ^= TOGGLE_BITS_MASK;            // Toggle bit 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