Set or Clear a Specific Bit in a Register

Code

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

int8_t ChangeBit(uint8_t num, uint8_t set, uint8_t bit){
    uint8_t temp = num;
    if(bit == 1){
        temp |= (1 << set);
      
    } else if (bit == 0) {
        temp &= ~(1 << set);
    } else {
        return 0;
    }

    return temp;
}

int main(){
    uint8_t decNum;

    scanf("%hhu", &decNum);

    uint8_t setBit;
 
    scanf("%hhu", &setBit);

    uint8_t bit;

    scanf("%hhu", &bit);


    uint8_t temp = ChangeBit(decNum, setBit, bit);


    printf("%hhu \n", temp);
}

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

10 3 1

Expected Output

10