#include <stdio.h>
#include <stdint.h>
uint8_t set_range(uint8_t reg, uint8_t start, uint8_t end) {
// Your code here
uint8_t len = end - start + 1;
uint8_t mask =((1<<len)-1)<<start;
return reg | mask;
}
int main() {
uint8_t reg, start, end;
scanf("%hhu %hhu %hhu", ®, &start, &end);
printf("%u", set_range(reg, start, end));
return 0;
}my approach here is , first we find the length of bits needed to make 1
we do by the following
length = end - start +1; (if end =3 , start =0 , len = 4(as per 0 indexing)
to find the mask ,
- 2^4 = 16 , 16-1=15=0x1111, so this gives us the mask of 4 bits,
- rather than using the powe function we take help of shift, as
1<<4 = 2**4 = 2^4 = 16
then obtained mask is shifted by start bit
mask is or with the register.