#include <stdio.h>
#include <stdint.h>
/*
uint32_t set_bits(uint32_t reg, uint8_t pos, uint8_t len) {
// Your code here
//First of all to set a bit we need OR
//To set consecutive bits
uint32_t temp = 0;
for (uint8_t i =0 ; i<len ; i++)
{
temp += (1<<(pos+i));
}
reg = reg | temp;
return reg;
}
*/
/*
uint32_t set_bits(uint32_t reg, uint8_t pos, uint8_t len) {
// Your code here
//First of all to set a bit we need OR
//To set consecutive bits
for (uint8_t i =0 ; i<len ; i++) //much simpler but failing case 5
{
reg += (uint32_t)(1<<(pos+i));
}
return reg;
}
*/
//Better way to do this without using the FOR loop:
uint32_t set_bits(uint32_t reg, uint8_t pos, uint8_t len) {
// Your code here
uint32_t temp = 0;
temp |= ((1<<(len)) - 1u); //The most simplest of all if you want to set consecutive bits
temp = temp << pos;
reg |= temp;
return reg;
}
int main() {
uint32_t reg;
uint8_t pos, len;
scanf("%u %hhu %hhu", ®, &pos, &len);
printf("%u", set_bits(reg, pos, len));
return 0;
}Input
0 4 3
Expected Output
112