Bit Operations using Macros

Code

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

// Define bitwise macros here
#define SET_BIT(reg, j) (reg |= (1 << j))
#define CLEAR_BIT(reg, j) (reg &= ~(1 << j))
#define TOGGLE_BIT(reg, j) (reg ^= (1 << j))



uint8_t modify_register(uint8_t reg) {
    // Apply operations in order
    uint8_t j = 0;
    for(int j = 0;j < 8; j++)
    {
        if(j == 2 || j == 7)
        {
            SET_BIT(reg,j);
        }
        else if(j == 3)
        {
            CLEAR_BIT(reg,j);
        }
        else if(j == 5)
        {
            TOGGLE_BIT(reg,j);
        }
    }
    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