All submissions

Bit Operations using Macros

Code

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

// Define bitwise macros here (Binary representation is important)
#define SET_BITS    0b10000100
#define CLEAR_BITS  0b00001000
#define TOGGLE_BITS 0b00100000

uint8_t modify_register(uint8_t reg) {
    // Apply operations in order
    reg |= SET_BITS;        // Set Bits

    reg &= ~CLEAR_BITS;     // Clear Bits 

    // Solution 1
    /*
    if(reg & TOGGLE_BITS){
        reg &= ~TOGGLE_BITS;        // If it is set, clear it
    }
    else{
        reg |= TOGGLE_BITS;         // If it is clear, set it
    }
    */
    reg ^= TOGGLE_BITS;

    return reg;
}

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

Solving Approach

 

 

 

Loading...

Input

0

Expected Output

164