14. Replace Bit Field in a 32-bit Register

Given a 32-bit register, replace a few bits (starting at position pos) with a new value.
Only the targeted bits must change — others should stay unchanged.

 
Example 1

Input: reg = 0b1111 1111, val = 0b0000, pos = 4, len = 4  
Output: 0b0000 1111

 
Example 2

Input: reg = 0b0000 1111, val = 0b10, pos = 1, len = 2  
Output: 0b0000 1101
Loading...

Input

255 0 4 4

Expected Output

15