Bit Operations using Macros

Code

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

typedef union {
    uint8_t value;
struct{
 unsigned SET_BIT_0 : 1; 
 unsigned SET_BIT_1 : 1; 
 unsigned SET_BIT_2 : 1; 
 unsigned SET_BIT_3 : 1; 
 unsigned SET_BIT_4 : 1; 
 unsigned SET_BIT_5 : 1; 
 unsigned SET_BIT_6 : 1; 
 unsigned SET_BIT_7 : 1;
}bit;
} Regiter;

uint8_t modify_register(uint8_t reg) {
   Regiter data ;
   data.value = reg;

   data.bit.SET_BIT_2 = 1;
   data.bit.SET_BIT_3 = 0;   
   data.bit.SET_BIT_7 = 1;
   if (data.bit.SET_BIT_5 == 1)
   {
    data.bit.SET_BIT_5 = 0;
   }
   else
   {
    data.bit.SET_BIT_5 = 1;
   }
    return data.value;
}

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