/* This code is to set specific bits in a 32bit register
submitted by Manish Gowda T
accomplished by leftshift and OR function*/
#include <stdio.h>
#include <stdint.h>
uint32_t set_bits(uint32_t reg, uint8_t pos, uint8_t len) {
uint32_t answer=reg | ((1<<len)-1)<<pos;
return answer;
}
int main() {
uint32_t reg;
uint8_t pos, len;
scanf("%u %hhu %hhu", ®, &pos, &len);
printf("%u", set_bits(reg, pos, len));
return 0;
}
Solving Approach
First we need to left shift by length then we subract by 1,to get number of bits to be placed then we move them by pos mentioned by user