Bit Operations using Macros

Code

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

// // Define bitwise macros here
// #define SETBIT_2_7(x) (x|=((1<<2)|(1<<7)))
// #define CLRBIT_3(x) (x&=~(1<<3))
// #define TOLBIT_5(x) (x^=(1<<5))

// uint8_t modify_register(uint8_t reg) 
// {
//     // Apply operations in order
//     SETBIT_2_7(reg);
//     CLRBIT_3(reg);
//     TOLBIT_5(reg);
//     return reg;
// }

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

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

// // Define bitwise macros here
// #define BIT(n) (1U << (n))

// #define SET(reg,mask) ((reg)|=(mask))
// #define CLEAR(reg,mask) ((reg) &= ~(mask))
// #define TOGGLE(reg,mask) ((reg) ^= (mask))

// #define MASK_SET (BIT(2) | BIT(7))
// #define MASK_CLEAR BIT(3)
// #define MASK_TOGGLE BIT(5)

// uint8_t modify_register(uint8_t reg) 
// {
//     // Apply operations in order
//     SET(reg,MASK_SET);
//     CLEAR(reg,MASK_CLEAR);
//     TOGGLE(reg,MASK_TOGGLE);

//     return reg;
// }

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

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

// Define bitwise macros here
#define BIT(n) (1U << (n))

#define SET(reg,mask) ((reg)|=(mask))
#define CLEAR(reg,mask) ((reg) &= ~(mask))
#define TOGGLE(reg,mask) ((reg) ^= (mask))

uint8_t modify_register(uint8_t reg) 
{
    // Apply operations in order
    SET(reg,BIT(2)|BIT(7));
    CLEAR(reg,BIT(3));
    TOGGLE(reg,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