Bit Operations using Macros

Code

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

// Define bitwise macros here
#define SET_BIT(value, pos) (value | (1 << pos)) 
#define CLEAR_BIT(value, pos) (value & ~(1 << pos))
#define TOGGLE_BIT(value, pos) (value ^ (1 << pos))

uint8_t modify_register(uint8_t reg){
    for (uint8_t i = 0; i < 8; i++){
        if (i == 2 || i == 7){
            reg = SET_BIT(reg, i);
        }
        else if(i == 3){
            reg = CLEAR_BIT (reg, i);
        }
        else if (i == 5){
            reg = TOGGLE_BIT(reg, i);
        }
    }
    return reg;
}

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

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

0

Expected Output

164