Set the Bit in an 8-bit Register

Code

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

//You are working with an 8-bit control register. 
//Write a function to set the bit at a given position without affecting other bits.
//Use 0-based indexing for bit positions (0 = LSB, 7 = MSB).

uint8_t SetBit(uint8_t reg, uint8_t pos){
    reg |= (1<<pos); 
    return reg; 
}

int main(){
    uint8_t reg,pos; 
    scanf("%hhu %hhu",&reg,&pos); 
    reg = SetBit(reg,pos);
    printf("%d",reg); 
    return 0;
}

Solving Approach
 

 

 

Upvote
Downvote
Loading...

Input

5 1

Expected Output

7