Code
#include <stdio.h>
#include <stdint.h>
uint32_t clear_bits(uint32_t reg, uint8_t pos, uint8_t len) {
// Your code here
int mask = 0;
for(int i=0;i<len;i++){
mask = mask | (1<<i) ;
}
mask = mask << pos;
return reg = reg & (~mask);
}
int main() {
uint32_t reg;
uint8_t pos, len;
scanf("%u %hhu %hhu", ®, &pos, &len);
printf("%u", clear_bits(reg, pos, len));
return 0;
}
#include <stdio.h>
#include <stdint.h>
uint32_t clear_bits(uint32_t reg, uint8_t pos, uint8_t len) {
// Your code here
int mask = 0;
for(int i=0;i<len;i++){
mask = mask | (1<<i) ;
}
mask = mask << pos;
return reg = reg & (~mask);
}
int main() {
uint32_t reg;
uint8_t pos, len;
scanf("%u %hhu %hhu", ®, &pos, &len);
printf("%u", clear_bits(reg, pos, len));
return 0;
}
consider example 1.
Input: reg = 0b1111 1111, pos = 4, len = 4
Output: 0b0000 1111
1. for(int i=0;i<len;i++){
mask = mask | (1<<i) ;
}
iterating the loop left shifting or(|) operation with the mask.
2. mask = mask << pos --> mask value rightshifting to position to value.
3. reg = reg & (~mask) --> complementing the mask value and (&) operation with reg
Input
255 4 4
Expected Output
15