Set Multiple Bits in 8-bit Register

Code

#include <stdio.h>
#include <stdint.h>
uint8_t set_multiple_bits(uint8_t reg, int start, int end){
    uint8_t mask=((1U << (end-start+1)) -1)<< start;
    reg |= mask;
    return reg;
}
int main(){
    uint8_t reg;
    int start,end;
    scanf("%hhu %d %d", &reg, &start, &end);
    printf("%u\n", set_multiple_bits(reg,start,end));
    return 0;
}

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

0 1 3

Expected Output

14